How to check if the navigation box is open?

I have activity with a navigation box. 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 the "Back" button, the navigation box closes if it is open, otherwise the method is called doExit();. But in my case, the else condition works fine, but when I click the back button when the box is open, the application closes completely. I executed this code How to determine if the navigation box is open?

Any help would be appreciated.

+4
source share
1 answer

DrawerLayouthas a method isDrawerOpen(listView)returns true or false, this will help you, I think

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

onBackPressed

+8

All Articles