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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…