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
How to Measure the Tilt of the Phone in Xy Plane Using Accelerometer in Android
How to Check If an Activity Is the Last One in the Activity Stack for an Application
Android Support Repo 46.0.0 with Android Studio 2.3
How to Capture an Image and Store It with the Native Android Camera
Android Studio Says "Cannot Resolve Symbol" But Project Compiles
Difference Between Oncreateview and Onviewcreated in Fragment
Android: How to Make a Clickable Map Image with Each Country Producing a Different Action
How to Send an Email with a File Attachment in Android
Switching Application-Wide Theme Programmatically
"Native Typeface Cannot Be Made" Only for Some People
Searchview in Listview Having a Custom Adapter
Why Does the Google Play Store Say My Android App Is Incompatible with My Own Device
About "_Id" Field in Android SQLite
How to Set Textview Textstyle Such as Bold, Italic
No Changes to Deploy Android Studio 2.1
Is Uploading Videos from an Sd Card to Facebook Possible with the Facebook Sdk