Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the navigation drawer is open?

I have an activity with navigation drawer. Below is my code.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(dLayout.isDrawerOpen(GravityCompat.START)==true) {
                 dLayout.closeDrawers();
                }

            else
            {
            doExit();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private void doExit() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                FirstSelection.this);

        alertDialog.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

According to this code, when the user presses back button, the navigation drawer is closed if it is opened, else the doExit(); method is called. But in my case, the else condition works fine, but when I press back button when the drawer is open, the complete app closes. I followed this code How to detect if navigation drawer is open?

Any help would be appreciated.

like image 459
Popsta Avatar asked Apr 20 '15 12:04

Popsta


1 Answers

DrawerLayout has method isDrawerOpen(listView) returns true or false this will help you I guess

if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } 

Add above code inside onBackPressed

like image 85
amodkanthe Avatar answered Sep 29 '22 14:09

amodkanthe