打开DrawerLayout时,Android onBackPressed不起作用

时间:2015-05-31 08:20:19

标签: android

在appliaction中我正在使用DrawerLayout来设置Sliding菜单,我想覆盖onBackPressed以关闭DrawerLayout(如果是opend),或者从应用程序关闭,但是onBackPressed在DrawerLayout打开时不起作用,我必须关闭,然后再按一下按钮

@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(UI.slide_menu)) {
        mDrawerState = false;
        mDrawerLayout.closeDrawer(UI.slide_menu);
    }
    if (G.back_pressed + 2000 > System.currentTimeMillis()) {
        super.onBackPressed();
    } else
        Toast.makeText(G.context, R.string.press_once_again_to_exit, Toast.LENGTH_SHORT).show();
    G.back_pressed = System.currentTimeMillis();
}

2 个答案:

答案 0 :(得分:1)

试试这个,

 if(mDrawerLayout.isDrawerOpen(Gravity.LEFT))
    {
        mDrawerLayout.closeDrawer(Gravity.LEFT);
    }

答案 1 :(得分:0)

试试这个为我工作onBackPressed

@Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
            mDrawerLayout.closeDrawer(Gravity.START);
        }else {
            super.onBackPressed();
        }
    }