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

前端 未结 8 1908
粉色の甜心
粉色の甜心 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:40

    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);
    }
    

提交回复
热议问题