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.
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();
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With