2

The situation could be like say, I've 5 activities. Say Main activity, Activity 1, Activity 2, Activity 3 and Activity 4.

One can use the Activity 1,2,3 & 4 directly with the help of buttons so inserted on Main Activity layout.

Situation: User enters into the Main Activity, and presses a button to enter into Activity 2. Then he back presses and comes back to Main Activity. from there he now enters Activity 3 with the help of another button and similarly when he presses back button he enters into Main Activity.

Main Activity --> Activity 2 --> Main Activity --> Activity 3 --> Main Activity.

Problem: Now as the user enters from Activity 3 to Main Activity. The user is on Main Activity layout. And now if the user presses the back button on its phone, then it will take the following process to exit the app:

Main Activity --> Activity 3 --> Main Activity --> Activity 2 --> Main Activity --> EXIT

Needs to be done: I want to exit the app from the Main Activity itself, if the user presses the back button on its phone, then instead of following the above path to exit the app, I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.

How could that be done, as I'm unable to find a solution to this problem?

Vibhor Gupta
  • 77
  • 1
  • 15
  • did you looked into this link https://stackoverflow.com/questions/2354336/android-pressing-back-button-should-exit-the-app – surhidamatya May 09 '18 at 04:42
  • https://stackoverflow.com/questions/20591959/android-quit-application-when-press-back-button – surhidamatya May 09 '18 at 04:43
  • For your first question check out the Android docs on Tasks and Back Stack: https://developer.android.com/guide/components/activities/tasks-and-back-stack This section is the most relevant: > When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. For your 2nd question on the alert dialog see the other comment. You will need to override onBackPressed in your activity and display your AlertDialog – Trevor Halvorson May 09 '18 at 04:46
  • possibility of dublicate https://stackoverflow.com/questions/6330260/finish-all-previous-activities – Amjad Khan May 09 '18 at 04:46
  • It worked..!! Thanks a lot @surhidamatya – Vibhor Gupta May 09 '18 at 04:51
  • @VibhorGupta you are welcome. Hope I could be helpful. Please add the answer worked for you as an answer in the thread so it could be helpful for others in future – surhidamatya May 09 '18 at 04:59

4 Answers4

5

You need to Override onBackPressed() inside your MainActivity() and use finishAffinity to finish all activities

onBackPressed()

Called when the activity has detected the user's press of the back key.

finishAffinity() : added in API level 16

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

SAMPLE CODE

@Override
public void onBackPressed() {
  super.onBackPressed();
  ActivityCompat.finishAffinity(MainActivity.this);
}

EDIT

I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.

@Override
public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Alert")
                .setCancelable(false)
                .setMessage("Are your sure want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.finishAffinity(MainActivity.this);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

        builder.show();

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

add your MainActivity

@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

It is a good code practice to write double back press to exit Main Activity.

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
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);

}

0

this is commonly used way to exit the app, also i recommend using "nohistory" attribute in manifest for activities other than MainActivity.

boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    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);

}

and for other activities use nohistory method in manifest

     <activity
        android:name="com.omniapay.salesservice.Activities.LoginActivity"
        android:label="LOGIN"
        android:noHistory="true" />
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61