Guys, you need to organize the exit from the application not with MainActivity, but with activity running, second, third, etc. When using the code below, the output is carried out in the previous activity .. I use the following code:

@Override public void onBackPressed() { new AlertDialog.Builder(this) .setTitle("Внимание!") .setMessage("Вы действительно хотите выйти?") .setNegativeButton(android.R.string.no, null) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Activity2.super.onBackPressed(); } }).create().show(); } 
  • In the Android system there is no concept "exit from the application", there are completions of actions. As soon as the last is completed, the application will disappear from the screen (but it will not be completed at all). That application can only complete the system (the legal way of course). - Eugene Krivenja
  • There is such a method Activity.finishAffinity() - it closes all activities, but only with API16 - woesss
  • Do you mean quitting the application when you press the Back button? (judging by the code) - Enikeyschik
  • Yes, on the Back button - ru38irk

1 answer 1

Complete all activations except the first:

 Intent intent = new Intent(getApplicationContext(), FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); 

And add the first activation to OnCreate :

 if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("EXIT", false)) { finish(); } 
  • one
    I add that FirstActivity must be on the stack (at the very bottom), otherwise it will not work. - Eugene Krivenja
  • something does not work .. again throws an en the same activity - ru38irk
  • Rather, throws on FirstActivity, which continues to work and be displayed on the screen - ru38irk
  • Did you add what I wrote to FirstActivity in OnCreate? - Cypher
  • Of course. The completion of the first activity is simply not practiced, the actions planned by the code of the first activity continue - ru38irk