0

I have an AlertDialog. I can get notified when the dialog is cancelled by installing a DialogInterface.OnCancelListener. However, I don't see any way to stop Android from automatically closing the dialog when the user presses the BACK button or taps outside of the dialog area.

Suppose that whenever the user tries to cancel dialog I want to show another dialog that asks "Are you sure you want to close this dialog?". I thought I could implement this in onCancel() in DialogInterface.OnCancelListener but it doesn't work because Android always automatically dismisses the dialog. Is there a way to stop Android from doing that so that I can choose whether I want to dismiss it or not?

Andreas
  • 9,245
  • 9
  • 49
  • 97
  • This is your answer - callback for outside touch event. https://stackoverflow.com/a/15411827/3186095 – MarcinR Jul 10 '18 at 13:14

3 Answers3

1

Try this:

private void openMainDialog() {
    new AlertDialog.Builder(this)
            .setTitle("Some title")
            .setMessage("Some Message")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    openOnCancelMainDialog();
                }
            })
            .show();
}
private void openOnCancelMainDialog() {
    new AlertDialog.Builder(this)
            .setTitle("Warning")
            .setMessage("Do you really want to close the dialog?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    openMainDialog();
                }
            })
            .show();
}
Prexx
  • 2,959
  • 5
  • 31
  • 48
  • But then I'd have to manually handle the closing logic. For the BACK button this is probably easily possible by overriding `onBackPressed` but what about tapping outside the dialog's area to cancel it? How should I detect this? – Andreas Jul 10 '18 at 13:01
  • I thought that's what you want? Prevent the dialog from closing if the user taps the outside or backbutton. – Prexx Jul 10 '18 at 13:04
  • Not quite. I want the dialog to be cancellable by tapping outside or pressing the back button but not immediately. When the user taps outside or presses the back button, I first want to show a confirmation dialog that asks: "Do you really want to close the dialog?" I'm looking for a way to implement this. – Andreas Jul 10 '18 at 13:06
  • Thanks, but this doesn't work. It shows the confirmation dialog but the main dialog goes away first. I want the main dialog to stay open and the confirmation dialog should be shown on top of it. – Andreas Jul 10 '18 at 13:21
0

However, I don't see any way to stop Android from automatically closing the dialog when the user presses the BACK button or taps outside of the dialog area.

There is a way to stop that,

 builder.setCancelable(false);

If that's what you were looking for, well then you got it. If you need any further details/questions, comment here, as your question ain't clear after that.

isamirkhaan1
  • 749
  • 7
  • 19
0

You can just disable a "random" cancelling of dialog. Use this method.

builder.setNegativeButton("Cancel", (dialog, which) -> {
    dialog.dismiss();
})    
builder.setCancellable(false);

Or you can use this method.

AlertDialog dialog = builder.show();
dialog.setCancelableOnTouchOutside(false);
Ufkoku
  • 2,384
  • 20
  • 44