In my activity, I have an instance variable of AlertDialog aDialog;
under some condition, my activity will pop up a dialog.
My question is if I create aDialog, do I need to dismiss() in my onDestory() or onPause() of my activity?
From my experience, no. If you've called aDialog.show() and it's still showing while the activity is ending or pausing, it may be good to call dismiss(). I don't remember too clearly, but I have had experiences where forgetting to dismiss/close a dialog when switching activities caused a slight undesired, but not problematic, flickering of the dialog on the next activity as it tries to force close the dialog. Hope this answers your question!
Related
Is it necessary to keep a reference to your spawned AlertDialog in your Activity and dismiss it in onDestroy() or could you just create it and forget it? It seems to be dismissed when your leave the Activity?
I suppose the AlertDialog holds a reference to the Activity Context normally, which might prevent the Activity from being garbage collected. What is the procedure to avoid possible memory leak here?
Does an AlertDialog belong to the Activity, i.e. does it follow the Activity lifecycle, being destroyed at the same time as the Activity and so on...?
I think, if you spawned the alert Dialog using the activity context or activity scope... It will be automatically destroyed whenever the activity is destroyed.
Maybe the problem arises when you want to spawn an Alert dialog from a reusable Utility class which doesn't have access to activity context, and ended up using Global Application context, then you need to manually destroy the dialog, since it may outlive the current activity, causing leak.
Can you please explain which activity life cycle method is called when a dialog comes on the application? I'm confused whether its is calling onResume() or onPause() method.
Thanks
OnPause() is not called in all types of dialogs.
For Example, when an AlertDialog or DialogFragment is used, it will never call OnPause(), since they are a part of the activity.
However, if a dialog appears from System for a permission or some other app shows a Dialog over the activity it will only call OnPause() since a new activity isn't started and only the foreground focus is shifted from the activity to the Dialog Box.
For Example, when we enable Whatsapp to send a message popup, if the popup comes while your activity is running, it will call OnPause() only.
You should try this on your own for better understanding.
Watch out, few of proposed answers are wrong. This old one have most of truth, but not whole truth. And that new one seems to complement my answer (haven't checked by myself).
This is not true that onPause is called after dialog appears. This dialog would have to be written on separate Activity to cause onPause call. But dialogs are usually written on DialogFragment from support library - reference
you should use a DialogFragment as a container for your dialog
Check also: Android: Under what circumstances would a Dialog appearing cause onPause() to be called?
onPause is not called because you are still in current activity, so when you are showing dialog on current activity no activity life cycle method will called.
Activity inside if open any dialog, then that dialog not affect to activity life cycle. so i already try this one. so onPause() not called. if any doubt please implement your self you can get more clarity.
I have checked the lifecycle of Activity is changed or not using a AlertDialog and I can see no changes in the lifecycle of activity. When a dialog appears no onPause is called and when a dialog is cancelled no onResume is called.
No lifecycle changes happens if the dialog is called
D/lc1: onCreate
D/lc1: onStart
D/lc1: onResume
I/System.out: lc1 AlertDialog is created
I/System.out: lc1 clicked yes
I/System.out: lc1 AlertDialog is created
I/System.out: lc1 clicked no
I/System.out: lc1 AlertDialog is created
I/System.out: lc1 clicked cancel
D/lc1: onPause
D/lc1: onStop
it calls onPause()
When a dialog comes on top of an existing activity, then existing activity will move to partially invisible state by calling onPause().
OnPause() is not called in all types of dialogs, check other answers for more details since I won't copy everything here.
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.
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().
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.
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()
I am asking this because I am always getting the Activity has leaked window.. error
Couldn't figure out a way to destroy some of the dialogs on activity destroy since these dialogs
are dynamically generated outside the activity(on some listener).
So is there a way for android to detect and kill all the visible/live dialogs?
Thanks
I believe that you can keep a member reference to your dialogs as you create them. Then you can check the isShowing() method to see if its showing. Then hide and destroy the dialogs that are showing.
This question is related to The AlertDialog is invisible when the Activity back to foreground post.
I have the same problem. The previous post is old, and have no answer. Any suggestions how to solve that problem ? Thanks...
For some reason, Dialogs' states must be handled by the developer.
Simply keep a reference to the dialog showing
For example
Dialog showingDialog=null;
Now in onResume()
if(showingdialog!=null)
//show the dialog and maybe resume some state
Have you tried to re-display the AlertDialog when in the activities onResume(). Using Google developers example you would be able to create an instance of this dialog and just recall it.
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Hope that helps.
Also we all create Dialogs on the fly whenever we need them, we should not.
Android way is (by the book) to override an onCreateDialog(int) and showDialog(int) in our activities so that our dialogs can be managed by the activity lifecycle.
Another way to do so is to use myDialog.setOwnerActivity(MyActivity.this) to tell the dialog it is managed by the activity.