1

I've done some reading about dialogs in Android and I have an open issue:

When I'm using the Activity's methods that handle the dialogs (such as: onCreateDialog(...)), should I or shouldn't I dismiss the dialog in the onPause()? Or maybe I should dismiss it only if I retained it - made an Activity member variable that has a reference to this dialog?

I've found this answer: https://stackoverflow.com/a/2851833/501560 saying that I need to explicitly call the dismiss() method, but I've read some other resources saying that the Activity should handle it by itself...

Thanks.

Community
  • 1
  • 1
ofirbt
  • 1,846
  • 7
  • 26
  • 41

3 Answers3

3

Dismissing a Dialog

When you're ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.

If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.

Using dismiss listeners

If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.

First define the DialogInterface.OnDismissListener interface. This interface has just one method, onDismiss(DialogInterface), which will be called when the dialog is dismissed. Then simply pass your OnDismissListener implementation to

setOnDismissListener().

However, note that dialogs can also be "cancelled." This is a special case that indicates the dialog was explicitly cancelled by the user. This will occur if the user presses the "back" button to close the dialog, or if the dialog explicitly calls cancel() (perhaps from a "Cancel" button in the dialog). When a dialog is cancelled, the OnDismissListener will still be notified, but if you'd like to be informed that the dialog was explicitly cancelled (and not dismissed normally), then you should register an

DialogInterface.OnCancelListener with setOnCancelListener().
jennifer
  • 8,133
  • 22
  • 69
  • 96
1

You never have to dismiss the dialog if it's managed by the Activity. The Activity will dismiss the dialog when it's destroyed. If the Activity is pause, Dialog doesn't have to be dismissed.

clemp6r
  • 3,665
  • 2
  • 26
  • 31
  • This is what I've suspected... Are u sure this is the way things happen? Do u have a link to a post or something with further explanations? Note my last edit to the question. – ofirbt Jan 04 '12 at 12:20
  • About your last question, no matter if you keep a reference to the activity or not, the Dialog will always reference your activity because it's its context. – clemp6r Jan 04 '12 at 12:26
0

I thought dialogs are supposed to be dismissed in onStop() or onPause() in the Activity lifecycle.

https://developer.android.com/reference/android/app/Dialog.html#dismiss()

RCB
  • 560
  • 4
  • 9