Different toolbar for fragments and Navigation Drawer

android, android-fragments, android-toolbar, navigation-drawer

Solution

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?

Problem

Please, explain to me... I have Navigation Drawer in my `Activity` and it syncs with `Toolbar` (like `ActionBar`). Activity has few fragments and in different fragments I need to use different `AppBar` modes (parallax in one, simple in another). So, I think that I should set `CoordinatorLayout` in each frament with AppBar and content. But how I can replace last toolbar on new to save synchronization with Drawer? Or it's wrong way and I need make it some else?

Original source