Prabs Prabs -4 years ago 121
Android Question

Navigation drawer in multiple activities

I know this question has been asked multiple times. But my concern is different.

Using Mr. Tamada Tutorial I've created a

and multiple
to replace in
. It's working fine.

Now, after selecting any option in a fragment, another activity gets open.

I want the same Navigation menu in that Activity.

Navigation view -- fragments -- Activity (display navigation view here)

What I tried:

  1. use the
    code of displaying
    Navigation view
    in that activity.
    (DrawerLayout, CoordinatorLayout, AppBarLayout etc)

    Then in, on click of menu item diverting to the Navigation Activity.



<?xml version="1.0" encoding="utf-8"?>



.... />


.... /> <!-- main content of this Acitivity-->


.... />

public void dashboard(MenuItem item) {
Bundle bundle = new Bundle();
bundle.putString("fragment", Constant.DASHBOARD_FRAGMENT);
UtilityClass.newActivity(this, NavigationActivity.class, bundle);

And handling the call on Navigation Activity. It is doing the task but code isn't re-usable

  1. Create a separate layout file for Navigation and include it in the Activity. But, this is replacing the main content. Here only included Navigation is visible.

Is there any other way to do it?

Answer Source

Downvoters - here the solution is..

  1. Create an xml file which will have DrawerLayout and NavigationView (one can use the xml given in Question, without the main content) - navigation.xml
  2. As suggested in many answers "create a BaseActivity which extends AppCompatActivity. And inflate navigation.xml.

    public class BaseActivity extends AppCompatActivity {  
        protected void onCreate(Bundle savedInstanceState) {  
            View view = View.inflate(this, R.layout.navigation, null);
            // view declarations
            DrawerLayout drawer = (DrawerLayout) view.findViewById(;
            NavigationView navigationView = (NavigationView) view.findViewById(;
            ...... }}
  3. In whichever Activity you wanna use this NavigationMenu, extend BaseActivity for that class.

    GraphActivity extends BaseActivity { .... }
  4. In GraphActivity.xml add the NavigationMenu code. You can't just include the navigation.xml it will disable the current xml widgets.

  5. Done!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download