233

Like the title says, what is the difference between a dialog being dismissed or canceled in Android?

5 Answers 5

217

Typically, a dialog is dismissed when its job is finished and it is being removed from the screen. A dialog is canceled when the user wants to escape the dialog and presses the Back button.

For example, you have a standard Yes/No dialog on the screen. If the user clicks No, then the dialog is dismissed and the value for No is returned to the caller. If instead of choosing Yes or No, the user clicks Back to escape the dialog rather than make a choice then the dialog is canceled and no value is returned to the caller.

4
  • 1
    Thanks for the reply. I'm trying to set what happens if the user pushes the cancel button on a datepicker. First I tried using a dismiss listener, and that worked, but also happened if the user chose a time. Then I tried a cancel listener, and it never went off, even if I pushed cancel. Do you know what I should do?
    – Aal
    Jun 26, 2010 at 22:49
  • 2
    If you're using a DatePickerDialog, you should use an OnDateSetListener to get notified when the date changes. If someone clicks the cancel button ... then it won't get called and the dialog will go away on its own. For normal uses of the DatePickerDialog, you don't need to set other listeners.
    – Lee
    Jun 27, 2010 at 1:16
  • FYI, the back button triggers both the cancel listener and the dismiss listener.
    – Urban
    Aug 17, 2018 at 20:15
  • What if i call cancel() instead of dismiss() Aug 12, 2020 at 12:46
34

dismiss is something you have to explicitly call in your code, usually to respond to a click event on a button in your Dialog. If you prefer, you can call dismissDialog in the Activity, which will in turn call dismiss on the Dialog.

The cancel method only executes when it is explicitly called in your code, or when the user presses the BACK button when your cancelable Dialog is open (as @Lee noted).

If you are using a DatePicker, then all of this is still the case. As @Lee said, DatePickerDialog.OnDateSetListener just detects when the user has chosen a date from the DatePicker.

The Android Developer Reference provides more info on Dialogs.

1
  • You can also call the cancel method. There are two different listeners for dimissing and canceling. Mar 25, 2018 at 16:02
32

Dismiss Calling the dismiss removes the dialog from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop.

Cancel Calling the cancel, cancels the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener, if registered.

Hide This method hides the dialog, but do not dismiss it.

enter image description here

And for more see here

2
  • 1
    cancel and dismiss both have callbacks: Cancel -> setOnCancelListener; Dismiss -> setOnDismissListener; May 31, 2021 at 14:41
  • 2
    @ВладиславШестернин absolutely and that's not a difference
    – ucMedia
    Jun 1, 2021 at 10:35
9

The difference is all about returning the value to the caller function.

dialog.cancel() will normally be called when the user hits the back button rather than selecting the choices that alert dialog offers like OK/Dismiss and return null/no value to the caller. While

dialog.dismiss() is normally called when the user selects from the choices that alert dialog offers like hitting the Dismiss button on the dialog will dismiss the dialog and will return the non-null corresponding value to the caller. That's it.

5

Giving this example

val dialog = AlertDialog.Builder(this@MainActivity)
    .setTitle("Title")
    .setMessage("Message")
    .setPositiveButton("Dismiss") { dialog, _ ->
        dialog.dismiss()
    }.setNegativeButton("Cancel") { dialog, _ ->
        dialog.cancel()
    }.create()

dialog.setOnDismissListener {
    Log.i("TAG", "onDismissListener")
}
dialog.setOnCancelListener {
    Log.i("TAG", "onCancelListener")
}
dialog.show()

When click Dismiss button

onDismissListener

When click Cancel button

onCancelListener
onDismissListener

When system Back button

onCancelListener
onDismissListener

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.