I have a main activity that is situated with two navigation drawers. The left one is always accessible, but the right one is used for displaying some necessary lists and perform
I was able to solve my problem by using DrawerLayout.DrawerListener
. This worked in my situation because when the back button was being pressed the drawer was still closing even though the method onBackPressed()
wasn't being called.
@MH said, "The DrawerLayout (I assume that's what you're using) will consume the back button event if the drawer is opened."
Although in the documentation I could not find an explicit mention of this; there was some mention of the back button here. Unfortunately it was not of much help.
What @MH said explains why onBackPressed()
was not being called when the drawer was opened, and why it was being called while it was closed.
drawerLayoutRight.setDrawerListener(this);
Where drawerLayoutRight
is a DrawerLayout
. And my listener looks like this:
@Override
public void onDrawerClosed(View arg0) {
if(drawerLayoutRight.getDrawerLockMode(drawerFrameRight)!= 1){
drawerLayoutRight.setDrawerLockMode(1, drawerFrameRight);
}
ExcerciseList fragment = (ExcerciseList) getFragmentManager().findFragmentById(R.id.right_drawer);
if (fragment != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(fragment).commit();
}
}
onDrawerOpened()
, onDrawerSlide()
and onDrawerStateChanged()
are all empty. Only one of my drawers is using the listener so I don't have to check the view.
I know it is a bit late but still... this answer will help others
@Override
public void onBackPressed()
{
if (mDrawerLayout.isOpen())
mDrawerLayout.close();
else
super.onBackPressed();
}
Quoting myself from the question at Force EditText to lose focus when back pressed
"In my experience onBackPressed() (at least the default @Override one in an activity) will not normally fire when pushing the back button to close the keyboard. As far as I know it will only fire when a Back press would initiate a finish() on the current activity."
This probably applies the same way to the navigation drawer.
This is most likely due to the fact that the current "Activity" is not in focus when the drawer opens (same with the SoftKeyboard) so the @Override back button is not called.
After adding this line back button will manage to close DrawerLayout
.
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
Just simple overrede your activity or drawerlayout, will do.
@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode
&& mDrawerLayout != null
&& mDrawerLayout.isDrawerOpen(leftDrawerView)) {
KeyEventCompat.startTracking(event);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override public boolean onKeyUp(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode
&& !event.isCanceled()
&& mDrawerToggle != null
&& mDrawerLayout.isDrawerOpen(leftDrawerView)) {
mDrawerLayout.closeDrawers();
return true;
}
return super.onKeyUp(keyCode, event);
}
Put this line into your code:
mDrawerLayout.setFocusableInTouchMode(false);
Then it will call your overwritten onBackPressed()
when you press the back button.