user4790312
user4790312

Reputation:

Android onBackPressed dont work when DrawerLayout is opened

in appliaction i'm using DrawerLayout to have Sliding menu, i want to override onBackPressed to close DrawerLayout if is opend, or close from application, but onBackPressed dont work when DrawerLayout is opend, i must be close that and try again to press back button

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

Upvotes: 0

Views: 256

Answers (2)

Saurabh Android
Saurabh Android

Reputation: 668

Try this its working for me onBackPressed

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

Upvotes: 0

Rishad Appat
Rishad Appat

Reputation: 1806

Try this,

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

Upvotes: 1

Related Questions