0

I am trying to exit an app when ever the user clicks the back button let it display a dialog yes/no button on clicking the yes button let it exit the app including with the registration activity assuming you are on the main activity.

This is my code but it not working

private AlertDialog AskOption()
{
   AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
       .setTitle("Exit") 
       .setMessage("Are you sure you want to exit?") 
      // .setIcon(R.drawable.delete)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int whichButton) {
//at this point i am trying to exit the app
               Register reg = new Register();
               reg.finish();
               finish();
               System.exit(0);

           }   
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               dialog.dismiss();
           }
       })
       .create();
       return myQuittingDialogBox;

   }

when the back button is clicked on the main activity

@Override
public void onBackPressed() {

    AlertDialog diaBox = AskOption();
    diaBox.show();

}

How do I exit the app from the mainactivity including the register activity on yes button click of the dialog

Blaze
  • 2,269
  • 11
  • 40
  • 82

5 Answers5

1
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int whichButton) {
           finish();
       }   
   })

Doing finish(); on click should be enough.

Douglas Fornaro
  • 2,017
  • 2
  • 22
  • 30
1

Use this code.

  .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
    //at this point i am trying to exit the app
              Intent intent = new Intent(Intent.ACTION_MAIN);
              intent.addCategory(Intent.CATEGORY_HOME);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(intent);
              finish();
              System.exit(0);

               }   
           })

Note :

if you used Intent.FLAG_ACTIVITY_CLEAR_TOP then it can clean the History of Activity.

  • @Blaze did you try it or not ? –  May 12 '16 at 12:34
  • This clears everything including any previous activity than just stepping in to include finish() at those previous activity. DRY principle – Blaze May 12 '16 at 12:35
  • Does this also remove the app from recents? – esfox May 12 '16 at 13:12
  • @esfox no this code not remove the app from the recent. for that you have to used `android:excludeFromRecents="true"`. in the Activity define in `Mainfest` –  May 12 '16 at 13:27
0

You are doing nice except following:

Register reg = new Register();

reg.finish();

Remove these code of lines and onBackButtonPress.

 Intent a = new Intent(Intent.ACTION_MAIN);
                    a.addCategory(Intent.CATEGORY_HOME);
                    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(a);
Community
  • 1
  • 1
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

Try this in your setPositiveButton

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int whichButton) {
           finish();
           System.exit(0);}}
Ramkumar.M
  • 681
  • 6
  • 21
0

System.exit(0); is only enough to exit from App. But finishing activity before exiting App would be good. So you can write the code like this,

.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) {

           finish();
           System.exit(0);

       }   
   })

It worked for me.

Jwala Kumar
  • 525
  • 7
  • 9