I am developing an android application. I wish that when I press back button in my application, it should give me a prompt(kind of alert dialog) if I really wish to exit. I dont know where to put this alert dialog and what to write in the Yes button, where user wants to quit the application. Please help me.
Asked
Active
Viewed 2,130 times
1
-
This question has been asked a lot [1](http://stackoverflow.com/questions/7550505/override-android-back-button) [2](http://stackoverflow.com/questions/4969541/override-the-back-button-in-android) [3](http://stackoverflow.com/questions/4779954/disable-back-button-in-android) – Andy McSherry Dec 18 '12 at 09:00
5 Answers
8
You can override OnBackPressed()
Function.
@Override
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Are You Sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
-
2Although every thing seems fine with this answer, it is not advisable to use System.exit(0); in android. – Android Dec 18 '12 at 08:42
-
1
3
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AppDemoActivity.this);
alertDialog.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setNegativeButton("No", null);
alertDialog.setMessage("Do you want to exit?");
alertDialog.setTitle("AppTitle");
alertDialog.show();
}
Use this to show an alert on Back Pressed

Rajeev N B
- 1,365
- 2
- 12
- 24
2
This is another way to Override Back button :
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
Thanks.

Pratik Sharma
- 13,307
- 5
- 27
- 37
1
You can override the onBackPressed
to show the alert, and in the yes
button finish
the activity..
public void onBackPressed() {
//show alert here
}

Nermeen
- 15,883
- 5
- 59
- 72
1
@Override
private void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Do you really want to exit?");
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
ThisActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

Rahul
- 10,457
- 4
- 35
- 55