Which is the good place to ask user, whether (s)he wants to exit the app when (s)he clicked the back button? I considered the onPause
and onStop
, but t
This is a rather old question but it ranks high on Google so I figured it could need an Update:
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to exit the app?");
// if user presses "Exit", then he is allowed to exit from application
builder.setPositiveButton("Exit", (dialog, which) -> finish());
// if user selects "Stay", just cancel this dialog and continue with app
builder.setNegativeButton("Stay", (dialog, which) -> dialog.cancel());
builder.create().show();
}