How to Show a Dialog to Confirm That the User Wishes to Exit an Android Activity

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

Use .setCancelable(false) with AlertDialog,

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// Your Dialog Code is here.

new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
ProfileActivity.super.onBackPressed();
finish();
}
}).create().show();
}

Confirm dialog before exit android studio


 private void confirmDialog(Context context){ 

final AlertDialog alert = new AlertDialog.Builder(
new ContextThemeWrapper(context,android.R.style.Theme_Dialog))
.create();
alert.setTitle("Alert");
alert.setMessage("Do you want to exit ?");
alert.setIcon(R.drawable.warning_icon);
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);

alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

alert.dismiss();

finish();

}
});

alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

alert.dismiss();

}
});

alert.show();
}

Call this method in

@Override
public void onBackPressed() {
super.onBackPressed();
confirmDialog(getApplicationContext());
}

of your Activity

Confirm back prompt to exit app

With the method Activity.isTaskRoot() you can check if the user will exit the app on pressing the back button.

Then you just have override onBackPressed() to handle it differently than usual.

Confirm Exit Dialog When Closing Android Studio

For MAC users follow the following steps:

1)Select Preferences from the Android Studio menu

2)Scroll down till the IDE Settings and select the sub section General

3)In the Startup/Shutdown section, you will find Confirm application exit. Select it to enable it again

4)Press Apply


For Windows users follow the following steps:

1)Select File from the toolbar

2)Select Settings

3)Scroll down till the IDE Settings and select the sub section General

4)In the Startup/Shutdown section, you will find Confirm application exit. Select it to enable it again

5)Press Apply

Android Exit button with alert dialog

remove super.OnBackPressed()

@Override
public void onBackPressed() {
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) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

}


Related Topics



Leave a reply



Submit