Muhammad Saidur Rahman
Muhammad Saidur Rahman

Reputation: 548

Handling back key for navigation drawer and fragment page

I am working on a app where I have few fragment pages also I have navigation drawer menu.

When I am on any fragments page and click on back key, application exit dialog is working fine. But when I am on any fragment page and at the same time if I click navigation menu, navigation menu is top position. Now if I click on back key, application exit button is coming but I want navigation menu should disappear first not the exit dialogue box.

Code for onBackPressed():

public void onBackPressed() 
{
        final FragmentManager fm = getSupportFragmentManager();

       if(fm.getBackStackEntryCount() != 1){
           super.onBackPressed();

        }
        else
        {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("Exit Application?");
            alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    moveTaskToBack(true);
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                    System.exit(1);
                                }
                            })

                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            dialog.cancel();
                        }
                    });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    }

==========================================================================

Code for onDrawerItemClick():

private void onDrawerItemClick(final int item) {
        UserService userService = new UserService(this);
        switch (item) {
            case TEST1:
                test1();
                break;
            case TEST2:
                test2();
                break;
            case LOGOUT:
                userService.logout();
                finish();
                Toast.makeText(MenuActivity.this, "LOGOUT", Toast.LENGTH_SHORT).show();
                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }

Upvotes: 0

Views: 707

Answers (3)

cake double
cake double

Reputation: 60

It's so easy. Let me show you my mind:

public void onBackPressed() 
{
    //The first line, you should check your drawer status.
    if(drawerIsOpen){
        closeDrawer();
    }
    else{
        //show your exit dialog.
    }
}

Upvotes: 0

Akshay Bhat 'AB'
Akshay Bhat 'AB'

Reputation: 2700

In onBackPressed() first you have to check if drawer is open, if it is open close it else do your normal stuff.

In your case like this :

public void onBackPressed() 
{
    final FragmentManager fm = getSupportFragmentManager();
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } else if(fm.getBackStackEntryCount() != 1){
       super.onBackPressed();
    }
    else
    {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Exit Application?");
        alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(true);
                                android.os.Process.killProcess(android.os.Process.myPid());
                                System.exit(1);
                            }
                        })

                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

Upvotes: 0

Rudresh
Rudresh

Reputation: 751

 @Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlayout);
    assert drawer != null;
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

Upvotes: 0

Related Questions