Different toolbar for fragments and Navigation Drawer

Not sure if my approach is good, but I tried to add this public method to my activity:

public void setToolbar(Toolbar toolbar) {
    if(toolbar != null) {
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    } else {
        drawer.setDrawerListener(null);
    }
}

and I added this in all fragments:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((MainActivity)getActivity()).setToolbar(toolbar);
}

@Override
public void onDestroyView() {
    ((MainActivity)getActivity()).setToolbar(null);
    super.onDestroyView();
}

It's working fine, but I'm not sure if it may cause a memory leak or any other performance issue. Maybe someone can help with it?


You can access main DrawerLayout from each Fragment just like the following code:

AppCompatActivity actionBar = (AppCompatActivity) getActivity();
actionBar.setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) actionBar.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

If you want to use appbar(toolbar) that you specified in navigation drawer in different fragments with, for example different options menu items, you could make a public method in Main Activity where you specified your navigation drawer logic.

public void setToolbar(Toolbar toolbar, String title){
    AppCompatActivity actionBar = this;
    actionBar.setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout)actionBar.findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    drawer.addDrawerListener(toogle);
    toogle.setDrawerIndicatorEnabled(true);
    toogle.syncState();
    if(toolbar != null)
        toolbar.setTitle(title);
}


Now you can use this method in your fragments to access the main toolbar and override it with your custom title, options menu... You can do this by creating a new toolbar variable in your fragment and then inflate it in onCreateView() method like this
toolbarFragment = (Toolbar)getActivity().findViewById(R.id.toolbar);

R.id.toolbar is the id of a toolbar that you specified in your layout file and it is the same id that you used in your main activity for the main toolbar.


Now you can call the method setToolbar(Toolbar toolbar, String title) in fragment like this
((MainActivity)getActivity()).setToolbar(toolbarFragment, "Some title");


If you want to use options menu for this fragment you have to call
setHasOptionsMenu(true);

in fragments onCreate() or onCreateView() methods. After that you can override method onCreateOptionsMenu() like this

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.custom_menu1, menu);
}

You can repeat this procedure for other fragments in your navigation drawer as well. Although it worked for me, I don't know if this violates activities or fragments lifecycle or causes memory leeks.