Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: onBackPressed() not being called when navigation drawer open

Tags:

android

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 performing certain actions. When I have certain fragments loaded into the center FrameLayout of the activity they have actions that require the right drawer be opened and an item be selected. All of this is working great, but I want to make sure that when either the back button is pressed or the screen is clicked outside the drawer that the drawer is closed, locked and the fragment loaded in there is removed. I should also mention that this right drawer holds a FrameLayout and relies on fragments.

In my main activity this is what I'm calling:

@Override       
public void onBackPressed(){
    Log.d(TAG, "onBackPressed() called");
    if(drawerLayoutRight.isDrawerOpen(drawerFrameRight)){
        Log.d(TAG, "----------------------");
        drawerLayoutRight.closeDrawer(drawerFrameRight);
        drawerLayoutRight.setDrawerLockMode(1, drawerFrameRight);
        ExcerciseList fragment = (ExcerciseList) getFragmentManager().findFragmentById(R.id.right_drawer);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.remove(fragment).commit();
    } else {        
        super.onBackPressed();
    }
}

I also tried:

public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.d(TAG, "onKeyDown called");
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

But this hasn't worked either. Whenever I click the back button the drawer will close, but neither of these methods would be called. But once the drawer is closed, if the back button is pressed these methods will be invoked. I was also wondering if anyone knew of a simple way to handle a click outside of the drawer. I figure I could make a custom DrawerLayout and override drawerClose() or check the coordinates that are clicked on a touch event. Just curious if anyone has a simpler way.

like image 217
Ryan Smith Avatar asked Aug 17 '13 22:08

Ryan Smith


3 Answers

Put this line into your code:

mDrawerLayout.setFocusableInTouchMode(false);

Then it will call your overwritten onBackPressed() when you press the back button.

like image 173
Neoh Avatar answered Nov 01 '22 22:11

Neoh


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.

like image 32
Shadesblade Avatar answered Nov 01 '22 20:11

Shadesblade


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.

like image 1
Ryan Smith Avatar answered Nov 01 '22 21:11

Ryan Smith