Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing NavigationView with all the Activities?

How do we share Drawer with all the activities?

In the lister: onNavigationItemSelected of setNavigationItemSelectedListener we can get the id and navigate to it. What I am looking for is something like this:

private void initDrawerLayout() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


        Intent intent;
        int id = menuItem.getItemId();

        switch(id) {

            case R.id.home:
            case R.id.drawer_home:
            // call activity instead of adding/replacing fragment
            return true;

            case R.id.drawer_gallery:
            // call activity instead of adding/replacing fragment
            intent = new Intent(MainActivity.this, GalleryActivity.class);
            startActivity(intent);
            return true;

            case R.id.drawer_about:
            // call activity instead of adding/replacing fragment
            intent = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(intent);
            return true;
        ...
        }
        ...

I know I can make all the menuItems add/replace Fragment, but then handling fragments and memory management is a big pain.

Instead I want each menuItem select/click to invoke Activity. i.e. each MainMenuItem to have Activity and those will hold fragments with complex layouts.

All I want to do is have each main menu item be an Activity instead of a Fragment.

And all these activities can share same DrawerNavigation.

Is this the recommended way? Or do we always add Fragments for NavigationDrawer item clicks??

Should I add NavigationView to BaseActivity and then extend all activities from there??

Following this new guide about Support Design lib

like image 683
Rinav Avatar asked Jun 13 '15 18:06

Rinav


People also ask

How do I use the same navigation drawer in all activities?

The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.

How do I add a navigation drawer to an existing activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


1 Answers

I found the answer using this SO answer

Extending is the right way. Just override setContentView in the right way...

like image 87
Rinav Avatar answered Oct 14 '22 00:10

Rinav