My code displays an AlertDialog, which exits the activity by pressing the positive button. I want it to be able to exit on back button as well. But my onBackPressed does not work when I have .setCancelable(false)
. How do I fix this without changing it to .setCancelable(true)
// show in dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("BROADCAST")
.setMessage(text)
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onBackPressed() {
finish();
}