I have developed an app working successfully, but there are two activities, first is the splash screen and second one is webview. Now problem is that when back button is pressed it closes the App and exits, however the back, forward and refresh button are there in the app for internal navigation. So how to show an are you sure to exit dialog or to prevent user from deactivating the back key.
Asked
Active
Viewed 1.4k times
4 Answers
11
Something like this by overriding onBackPressed()
method of Activity
class:
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}

nikis
- 11,166
- 2
- 35
- 45
1
Just do,
@Override
public void onBackPressed() {
// super.onBackPressed();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle("Exit alert");
alertDialog.setMessage("Do You want Exit??");
alertDialog .setIcon(R.drawable.galleryalart);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
[youractivity].super.onBackPressed();
}
});
alertDialog.show();
}

Sekhar Madhiyazhagan
- 869
- 5
- 14
0
Override onBackPressed in your activity
@Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Signout your app");
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}

playmaker420
- 1,527
- 4
- 27
- 52
0
Try this way
public class YourActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int imageResource = android.R.drawable.ic_dialog_alert;
Drawable image = getResources().getDrawable(imageResource);
builder.setTitle("Exit").setMessage("want to exit?").setIcon(image).setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}).setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.setCancelable(false);
alert.show();
}
}

Biraj Zalavadia
- 28,348
- 10
- 61
- 77