Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit app when press Back button in activity with multiple fragments in Navigation Drawer

I'm working on an app that need Navigation Drawer. This app has 1 MainActivity and about 10 fragments on it. When I click the Navigation Drawer icon in MainActivity it will show the 10 fragments so I can select each of them.

I choose fragment A, then B, then C...to F, for example. When I'm in fragment F and press the Back button, it will take me back to fragment E, then Back again it will take me to fragment D...

My problem is when I'm back to fragment A (from fragment B, of course) and press Back button one more time, it take me to a blank white screen (I think this is the main activity layout). And press Back one more time, the app will exit.

What I want is when I'm back to fragment A (last fragment) and hit Back, the app will exit immediately, not the blank white screen anymore

I've search on SO and found 1 similar question but no answer yet so I have to make another question

How can I do this? Thank you very much

like image 639
Hoang Trinh Avatar asked Jun 18 '14 04:06

Hoang Trinh


2 Answers

Very late answer. But I hope atleast help this answer anyone.

With help of following code I implement the double back press exit (It give Toast "Please click back again to exit" if press once more only app will exit.)

getBackStackEntryCount() - give the current count of your back stack fragments.

int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();

if this value==0 no fragments avilable in back stack. so execute the double back press exit code.

else allow the backpress action using super.onBackPressed();

@Override
public void onBackPressed() {

    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
    //backStackEntryCount==0 -> no fragments more.. so close the activity with warning
    if (backStackEntryCount == 0){

        if (homePressed) {
            if (doubleBackToExitPressedOnce) {
                super.onBackPressed();

                return;
            }

            this.doubleBackToExitPressedOnce = true;
            Toast.makeText(this, "Please click back again to exit", Toast.LENGTH_SHORT).show();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    doubleBackToExitPressedOnce = false;
                }
            }, 2000);
        } else {
            homePressed = true;
        }

}
    //some fragments are there.. so allow the back press action
    else {
        super.onBackPressed();
    }

}    

Globally declare these variables

boolean homePressed = true, doubleBackToExitPressedOnce = false;
like image 143
Ranjithkumar Avatar answered Sep 26 '22 14:09

Ranjithkumar


just exit the app at second last screen

  public void onBackPressed() {

    if (manager.getBackStackEntryCount() > 1 ) {
        // If there are back-stack entries, leave the FragmentActivity
        // implementation take care of them.
        manager.popBackStack();

    } else {
        // Otherwise, ask user if he wants to leave :)
        new AlertDialog.Builder(this)
                .setTitle("Really Exit?")
                .setMessage("Are you sure you want to exit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        MainActivity.super.onBackPressed();
                    }
                }).create().show();
    }
}
like image 39
Ali Azhar Avatar answered Sep 24 '22 14:09

Ali Azhar