Exit an app on dialog button click

alertdialog
custom exit dialog android
exit alert dialog android
angular modal close when click outside
android exit app
mat-dialog close when click outside
android custom popup dialog example
android confirmation dialog

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

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.

How To Add Android Exit Dialog To Android App Using Android Studio, If you wish to use C++ for coding the project, mark the "Include C++ support" box, and click the "Next" button. Android. Now, select the version of� Finish method closet all the all open existing activities and exit application user. So in this tutorial we are exiting from MainActivity on button click method. So here is the complete step by step tutorial for Exit/Close android app programmatically on button click.

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

Doing finish(); on click should be enough.

Alert Dialog Tutorial With Example In Android Studio, For example you have seen this type of alert when you try to exit the App and App a simple button and on that button click the alert dialog window will appear. Exit () Exits the current app and leaves the user signed in. The user is returned to the list of apps. Exit ( true ) Exits the current app and the user is signed out. The user will need to sign back in with their credentials before running an app. Related Articles.

You can do it like this

@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
       .setMessage("Are you sure you want to exit?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                ExampleActivity.this.finish();
           }
       })
       .setNegativeButton("No", null)
       .show();
}

And for the activity that remain open when you click on yes after this code. For that you have to finish() after startActivity().

How to Close Dialog when click outside of its region in Angular , By default, dialog can be closed by pressing Esc key and clicking the close icon @Component({ selector: 'app-root', template: `<button class="e-control e-btn"� To close a running app with a button simply put the following function in the OnSelect of a button: Exit() This will close the app on desktop or mobile. Also it only works in published version of the app (you will not be able to use this button while in editor in preview mode)

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);

How to Close Dialog while Click on Outside of Dialog in Vue Dialog , By default, dialog can be closed by pressing Esc key and clicking the close icon on the { ButtonPlugin } from '@syncfusion/ej2-vue-buttons'; Vue.use( DialogPlugin); node_modules/@syncfusion/ej2-vue-popups/styles/material.css "; #app� In my application i want exit from app when press back button, this my code: @Override public void onBackPressed() { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_

Try this in your setPositiveButton

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

Exit Dialog for Android Application, This is an example of implementing simple Exit Dialog with two options, YES, and NO. public void onClick(DialogInterface dialog, int id) { Click back button twice to exit the applicationNovember 14, 2019In "Android". Every dialog should contain a safe, nondestructive action button that enables the user to confidently exit the dialog. Use the ContentDialog.CloseButton API to create this button. This allows you to create the right user experience for all inputs including mouse, keyboard, touch, and gamepad. This experience will happen when:

Dialog, app.dialog.get(el)- get Dialog instance by HTML element. el - HTMLElement close, boolean, true, If enabled then button click will close Dialog. cssClass, string� We add OnClickListener function to the button so whenever user clicked on the button a dialog will popup with a warning "Do you want to exit?". Two options will be provided to the user, "Yes" to exit and "Not" to close the dialog and go back to main.

How to close my app when I click button back in xamarin form , Your code is perfect for closing app. It might be becaause of thread can you try this. Hide Copy Code. if (AcceptBack) return false; Device. The null parameters are actually the onClickListeners for these buttons. We are doing this, because later on we will override the behavior of these buttons. Of course, you can try to provide click listeners there, but no matter what you will include in these listeners, your AlertDialog will disappear after the event.

HelloWorld With Exit Button AndroidStudio : 4 Steps, HelloWorld With Exit Button AndroidStudio: This tutorial will teach you on how to build Android App that display Hello World text and Exit button to exit from the activity. OnClickListener() { @Override public void onClick(DialogInterface�

Comments
  • new Register();... Never make an Activity like that
  • check for back stack count the count is 0 then activity.finish will do
  • I think it could be duplicated with this: stackoverflow.com/questions/20591959/…
  • @Blaze did you try it or not ?
  • This clears everything including any previous activity than just stepping in to include finish() at those previous activity. DRY principle
  • Does this also remove the app from recents?
  • @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
  • This navigates me to the Register activity
  • @Blaze you have to finish application when you navigate from register activity to another activity
  • You approach is equally correct. Finishing activity before moving to the next
  • This takes me back to the register activity