I use the main activity (with MaterialDrawer) as a container for fragments. After switching to the next fragment I change home button to back arrow and lock MaterialDrawer:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
But I can't understand how to catch back arrow click. Always by clicking on the back arrow opens MaterialDrawer.
@gmikhail if you provide the Toolbar to the DrawerBuilder it will add an ActionbarDrawerToggle for you which will open / close the drawer.
For the ActionBarDrawerToggle you might can check this:
http://iamtechnologist.blogspot.co.at/2015/03/how-to-completely-disable-android.html
@mikepenz I'm not really asking this, but in any case - thanks for the help! The solution is to set the listener directly to a toolbar object. If someone will be useful, here's the code that I use to switch between the fragments:
// Enable up button only if there are entries in the back stack
boolean isCanBack = mFragmentManager.getBackStackEntryCount() > 0;
if(getSupportActionBar() != null) {
if(isCanBack) {// Show back arrow and lock MaterialDrawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // Home button listener
if(mFragmentManager.getBackStackEntryCount() > 0) {
mFragmentManager.popBackStack();
} else mDrawer.openDrawer();
}
});
} else {// Show hamburger and unlock MaterialDrawer
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mDrawer.getActionBarDrawerToggle().syncState();// If not call will be displayed back arrow, not hamburger
mDrawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
Most helpful comment
@mikepenz I'm not really asking this, but in any case - thanks for the help! The solution is to set the listener directly to a toolbar object. If someone will be useful, here's the code that I use to switch between the fragments: