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

前端 未结 8 1910
粉色の甜心
粉色の甜心 2021-02-05 05:54

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

8条回答
  •  一整个雨季
    2021-02-05 06:31

    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.

提交回复
热议问题