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.
Related
Will my Android Activity always call onResume() when it goes to the foreground?
Yes, based on Activity lifecycle.
First of all, I suggest to look at the Android Activity Life-cycle management.
http://developer.android.com/training/basics/activity-lifecycle/starting.html
The Activity Lifecycle always rest till onResume() method when the screens comes fully visible on the foreground otherwise it may stop at onStart() when the screen view is partially visible possibly blocked by a modal dialog.
I have read documentation on onResume() and onStart() but one thing I'm still not cleared is under what scenario does onResume() get called without onStart() being called before it?
Please Refer to the Android Activity Lifecycle Documentation.
onStart is called when your application first starts.
If the user click the home button, or another app takes focus, onPause will be called.
If the activity regains focus, while stil running on the device, onResume will be called, and onCreate will NOT be called again.
If the user uses the activity manager to close the application, and then relaunches it, onCreate will be called again.
Note, every time onCreate is called, onResume is also called.
Check below chart:
In case your activity is visible but not active - onPause will be called, and then when you return to this Activity - onResume
One such scenario where onResume() is called without onStart() being called is change of Focus. Think about a dialog popping up on the screen while you're using the application. This is the case when onPause() is called, followed by onResume() after dismissal of dialog.
onStart() gets called once each time the app is launched and is actually called after oncreate()
onResume() gets called instead if the app is already running, just in the background.
if you use onPause(), on Resume will likely get called when you bring your app up again, basically onResume() is a reusable onStart() for when the app is already started.
onResume can sometimes be called when switching activities, onStart only gets called when creating one.
onResume() is called without onStart() when the activity resumes from the background.
I am new in android and I am learning from developer.android.com site. Then I came across to AlertDialog.dismiss() where in site it is written that
This method Dismiss dialog and remove it 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().
But I did not understand the mean of this line-
Note that you should not override this method to do cleanup when the
dialog is dismissed, instead implement that in onStop()
what is the mean of above line?
`.
AlertDialog.dismiss() uses to dismiss the dialog if it's opened up as describe at developer site
Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
The above statement simply means that as we used to garbage collect object which is no more referenced in class and avail for garbage collect. They are simpling stating that the approach like avail for garbage collection also applies in here but there are eligible inside onStop() of Activity.
So better to use it as onStop() as it's the last call of Activity Life Cycle which can dismissed your alertdialog. If it incase is there on the screen without dismissal.
I use code below to start an activity:
Intent intent = new Intent(XXX.this, YYY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(data);
somehow, I don't know why, XXX's onResume() method is invoked.
and because I try to pop up a progressDialog inside onResume(), I got below error:
`android.view.WindowLeaked: Activity XXX has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#406ca468 that was originally added here.`
In debug mode, I set a breakpoint at the first line of method onResume(), it never stops there, but still I get the above error.
why does this happen?
Because onResume is part of the Activity lifecycle, it will be called regardless of whether this is the first time your Activity is being created. If you want to open a dialog when the user returns to it, you should probably put that behaviour in onRestart, not onResume.
Edit: Without seeing your dialog creation code, it's hard to say what causes the error. However, make sure you are placing a call to showDialog and returning a created Dialog from onCreateDialog, not just creating the Dialog itself in onResume.
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!