I have an AlertDialog that shouldn't close when a certain condition, (a button of it isn't enabled) happens if the back button of the device is pressed.
With the following code, I've managed to partially achieve the desired behavior.
dialog1.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
Button button3 = ((AlertDialog)
dialog1).getButton(AlertDialog.BUTTON_NEUTRAL);
if (!button3.isEnabled())
{
dialog1.show();
}
else
{
dialog1.dismiss();
}
}
});
But this code presents 2 problems:
1) There's a small time where dialog1 stops showing to show again, this looks a bit bad.
2) Much more important, one needed button that displays when that button is disabled stops showing, this button doesn't initially show with the dialog, under some circumstance which also triggers that the shown button gets disabled it gets to show. For some reason, it looks like the dialog isn't refreshed to its last state and keeps only the elements that originally had.
Is there anyway so that if the back button is pressed when showing the dialog under the mentioned condition absolutely nothing happens or at the very least to keep the exact same elements it had when dismissed and is later shown again?
Use setCancelable();
Sets whether the dialog is cancelable or not. Default is true.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
this will not let you click outside the dialog to dismiss it, or simply go back closing it
dialog1.setCancelable(false);