Arshia Nikoosiar
Arshia Nikoosiar

Reputation: 11

Can I extend custom things (like only nav drawer) from one activity to another one?

I have 2 activities: SplashActivity and MainActivity. I make a drawer menu in MainActivity, and include a content.xml in there.

I want to import the drawer menu to all my activities that I make in the future.

I make a new activity like IntroActivity and extend it. Actually, I don't want to see content.xml that is included in MainActivity in IntroActivity and only want to see the drawer and in IntroActivity.

This is my mainActivity.java:

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    protected DrawerLayout drawerLayout;
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

        TextView tv = (TextView)findViewById(R.id.textView);
        Typeface textfont = Typeface.createFromAsset(getAssets(),"fonts/B Roya_YasDL.com.ttf");
        tv.setTypeface(textfont);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.a1);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
        navigationView = (NavigationView) findViewById(R.id.a2);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setChecked(true);
                drawerLayout.closeDrawer(Gravity.RIGHT);
                switch (item.getItemId()){
                    case R.id.intro:
                        Intent i = new Intent(MainActivity.this, IntroActivity.class);
                        startActivity(i);
                        break;
                }



                return false;
            }
        });

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.khat) {
            if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                drawerLayout.closeDrawer(Gravity.RIGHT);
            }

            else {
                drawerLayout.openDrawer(Gravity.RIGHT);
            }
            return super.onOptionsItemSelected(item);
        }
        else {
            Toast.makeText(getApplicationContext(),"Somthing",Toast.LENGTH_LONG).show();
        }

        return true;
    }

    public void onBackPressed(){
        if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
            drawerLayout.closeDrawer(Gravity.RIGHT);
        }else {super.onBackPressed();}

    }
}

This is my IntroActivity:

public class IntroActivity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro2);
    }
}

Can I do something to achieve my goal?

Upvotes: 0

Views: 55

Answers (1)

Marius Andrei Rosu
Marius Andrei Rosu

Reputation: 80

Your IntroActivity should not extend your MainActivity because IntroActivity IS NOT a MainActivity type object.

To have the navigation drawer functionality into one or more activities you should create an abstract activity class, NavigationDrawerActivity, and move all the code for the navigation drawer from MainActivity there. Then make IntroActivity and MainActivity extend NavigationDrawerActivity.

This way both your activities will have the navigation drawer functionality and won't share them unnecessarily, like the content.xml layout.

Upvotes: 1

Related Questions