How to show a dialog to confirm that the user wishes to exit an Android Activity?

exit alert dialog android
android code to exit application
exit dialog android github
android show alertdialog on back pressed
double back press to exit android
confirmation message android studio
android dialog activity
android studio exit app

I've been trying to show a "Do you want to exit?" type of dialog when the user attempts to exit an Activity.

However I can't find the appropriate API hooks. Activity.onUserLeaveHint() initially looked promising, but I can't find a way to stop the Activity from finishing.


In Android 2.0+ this would look like:

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Closing Activity")
        .setMessage("Are you sure you want to close this activity?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }

    })
    .setNegativeButton("No", null)
    .show();
}

In earlier versions it would look like:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.quit)
        .setMessage(R.string.really_quit)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                //Stop the activity
                YourClass.this.finish();    
            }

        })
        .setNegativeButton(R.string.no, null)
        .show();

        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }

}

How To Add Android Exit Dialog To Android App Using Android Studio, In this article, I will show you 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++ Now, add the activity and click the "Next" button. This example demonstrates how do I show a dialog to confirm that the user wishes to exit an Android Activity. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.


@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.super.onBackPressed();
               }
           })
           .setNegativeButton("No", null)
           .show();
}

Exit Confirmation Dialog Box For Android Application, This example demonstrates how do I show a dialog to confirm that the user wishes to exit an Android Activity.Step 1 − Create a new project in  A blog post entitled Exiting Android Application will show how to exit an Android app:. When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case “LoginActivity”.


Have modified @user919216 code .. and made it compatible with WebView

@Override
public void onBackPressed() {
    if (webview.canGoBack()) {
        webview.goBack();

    }
    else
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();
alert.show();
    }

}

Android How to show dialog to confirm user wishes to exit Activity, A Dialog is small window that prompts the user to a decision or enter activity and without Duration: 8:45 Posted: 16 Jun 2018 In this tutorial u will learn how to exit from app on back press with dialogue box please share this tutorial with your friends thank you!! Support my channel by donation my paypal link is below


I'd prefer to exit with double tap on the back button than with an exit Dialog.

In this solution, it show a toast when go back for the first time, warning that another back press will close the App. In this example less than 4 seconds.

private Toast toast;
private long lastBackPressTime = 0;

@Override
public void onBackPressed() {
  if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
    toast = Toast.makeText(this, "Press back again to close this app", 4000);
    toast.show();
    this.lastBackPressTime = System.currentTimeMillis();
  } else {
    if (toast != null) {
    toast.cancel();
  }
  super.onBackPressed();
 }
}

Token from: http://www.androiduipatterns.com/2011/03/back-button-behavior.html

Alert Dialog Tutorial With Example In Android Studio, Android How to show dialog to confirm user wishes to exit Activity. What is the AlertDialog? The AlertDialog class allows you to build a variety of  I am trying to create an AOSP with a boot logo. I also have an android app which is persistant app preinstalled. I want the boot logo to be update anytime later with just an update of the said pers


If you are not sure if the call to "back" will exit the app, or will take the user to another activity, you can wrap the above answers in a check, isTaskRoot(). This can happen if your main activity can be added to the back stack multiple times, or if you are manipulating your back stack history.

if(isTaskRoot()) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                YourActivity.super.onBackPressed;
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();
    alert.show();

} else {
    super.onBackPressed();
}

'Exit Application?' Dialogs Are Evil, Don't Use Them!, Learn how to use alert dialog with example in Android Studio. making a decision it is required to give an alert to the user without moving to next activity. seen this type of alert when you try to exit the App and App ask you to confirm exiting. That is a simple alert dialog, Federico gave you a site where you can look things up.. Here is a short example of how an alert dialog can be built. new AlertDialog.Builder(this) .setTitle("Title") .setMessage("Do you really want to whatever?") .setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick


How to show a dialog to confirm that the user wishes to exit , I've been trying to show a “Do you want to exit?” type of dialog when the user attempts to exit an Activity. However I can't In Android 2.0+ this would look like:


How+to+show+a+dialog+to+confirm+that+the+user+wishes+to+exit , Should I implement an “Exit application?” dialog in prompted with a dialog, questioning whether or not the user wishes to exit the application: 


R.drawable.ic_dialog 2.Android How to show dialog to confirm user wishes to exit Activity Description:04-10-2012 · The best option to show a