In my simple android app I have 2 activities. In first activity(Main activity) I have override the ON STOP method. But when I go do the second activity on stop method of main_Activity is called. Why this happens? According to my understanding I should call on Pause rather then on Stop.
It calls both. onPause() will be called before onStop(), if onResume() was called. Rougly speaking, onPause() is called when activity is partially hidden, and onStop() when it is fully hidden.
Read up on http://developer.android.com/training/basics/activity-lifecycle/index.html to understand the Activity lifecycle.
onPause() - activity is paused.
onStop() - activity is stopped working but it is there in stack.
onDestroy() - activity is stopped and also has been removed from stack.
therefore When you go from one activity to another activity, it makes onPause() and onStop() but not onDestroy()
OnStop is called when FirstActivity calls SecondActivity and FirstActivity looses
visibility.
If Second Activity has a transparent background then the FirstActivity will be visible underneath, so not loosing visibility and onStop on FirstActivity will never be called.
Related
I know that In Android, you won't get a guaranteed activity's onDestroy() call. My question is in which scenarios onDestroy() is not called?
Suppose there are two activities A and B.Activity A contains a next button and B contains a back button.If you click next,then Activity B will be launched.At that point of time onDestroy() is not called.onStop() of activity A is called after onResume() of B.
Then when you click the back button in Activity B,then onRestart() of A will called after onStop() of B.In this case also onDestroy() will be not called.
When you press the back button of the device onDestroy() will be called at that point of time or if you call finish() while navigating from A to B.
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.
In the Android developer diagram, I saw that onResume() is always called before onPause(). Assuming the user starts a new Activity, why should onPause() be preceded by onResume()?
I mean:
OnResume can be called in 2 occassions:
1) when user starting new activity (before OnPause)
2) when activity is in background and if the user brings the activity back to the
foreground
I expect in every case, something else should be done.
You are getting it wrong. Whenever an activity is created, onResume is called just after onStart. Whenever your activity goes back stack onPause is called. Again if your activity comes back to foreground then onResume is called. So, it is not like, onResume is called before onPause. Whenever activity is returning from onPause state, onResume gets called instead of onStart or onCreate. This happens so that Android does not have to create Activity instance again and again though those instances are not properly destroyed. This is quite memory efficient too.
NOTE: If you app is running and the user presses back button or home button, then the Activity goes through onPause() and onStop() state. After this if the user is again coming back to your app then, onRestart(), onStart() and onResume() will be called sequentially.
Then when the activity is only in onPause() state ? When a dialog surfaces on top of your activity or your activity is getting displayed in Split screen but it doesn't have focus (user is not interacting with your app). On these cases, activity goes to onPause() state only.
onResume() is always called before onPause()
This is correct. onResume is always called when the Activity is launched for the first time, before onCreate, and when the Activity is being resumed (user navigates back to your Activity)
Assuming the user starts a new Activity, why should onPause() be
preceded by onResume()
onPause is only called when the Activity is put to background, or before onDestroy if the Application is being destroyed. So onPause is always being called after a call to onResume has been made. Why? Because that's the lifecycle of the Activity as defined by the Android framework.
The life cycle of the activity is as follows
Fresh start via onCreate(), onStart(), onResume .... and close via onPause()->onStop()->onDestroy()
Yellow background: Activity goes into background and thus is no longer visible. The user returns back to the activity.
e.g.
Switch off the phone screen while the activity is running: onPause()->onStop()
Switch on the screen again: onStart() -> onResume()
Green background: The activity stays in the visible screen area but is not active
e.g. Activate multiple windows (split screen) occupying one part of the screen each and tip on your app to make it active
tip on the other app: onPause() is called in your app as it goes into pause but is still visible
tip on your app: onResume() is called
Here is an example of a split screen with two apps:
see android documentation on activity life cycle for details
I am new to android.I have learned that the onPause() method is called when some other activity comes in the foreground and onStop() method is called when an activity is no longer visible.Can someone please explain what's the difference between going in background and becoming invisible giving some practical example.
Best example? A popup, your activity is still visible, so only onPause() is called. Becoming invisible is when another activity is launched, the app is sent to the background, the user switches to another app et.
Think about two cases
1. Calling Activity B as dialog
In this case, some part of Activty A will be visible. So only onPause() method will get called for Activity A. And when you will be back from Activity B to A, onResume() of Activity A will get called.
2.Calling Activity B as normal activity
In this case, Activity A will be completely invisible, So onPause() and onStop() both will be called. And when you will be back from Activity B to A, onRestart(), onStart() and onResume() methods of Activity A will be called.
Have a look
Case 1: If you call normal activity. (Which is going to hide caller activity)
onPause()
onStop()
<------- Back to caller
onRestart()
onStart()
onResume()
Case 2: If you call activity-as-dialog. (Which is going to partially hide caller activity)
onPause()
<------- Back to caller
onResume()
whenever a dialog box appears on any activity then activity's onPause() called and this activity holds the first position in stack(Top), on the other hand whenever we switches activity from one to another the previous activity's on Stop() method called and its automatically garbage collected in sometimes.
If I launch Activity2 from Activity1 by this way: startActivity(Activity2); what executes first: onStop() (Activity1) or onStart() (Activity2) ?
Do they work simultaneously or in turn? If one after another, what is first?
So in general : what is the activity's state order when first activity starts second, if this order exists?
Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this :-
A onCreate()
A onStart()
A onResume()
Now Button click for startActivity(intent)
A onPause()
B onCreate()
B onStart()
B onResume()
A onStop()
..... If you press back button from Activity B then lifeCycle call will be .....
B onPause()
A onRestart()
A onStart()
A onResume()
B onStop()
B onDestory()
Now one more scenario "From Activity A start Activity B by calling StartActivity(Intent) on button click and use finish() method inside onstart() method on Activity B"
A onPause()
B onCreate()
B onStart()
A onResume()
B onStop()
B onDestory()
According to the documentation, the onStart on Activity2 is called before onStop on Activity1 (or, if you prefer, the os waits onStart on Activity2 to be finished before calling onStop on Activity1).
From http://developer.android.com/guide/topics/fundamentals/activities.html:
The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.
when I have checked it by programmatically its following all steps and easy to understand
Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this:-
A onCreate(), A onStart(), A onResume()
Now Button click for startActivity(intent)
A onPause(), B onCreate(), B onStart(), B onResume(), A onStop()
If you press back button from Activity B then lifeCycle call will be .....
B onPause(), A onRestart(), A onStart(), A onResume(), B onStop(), B onDestory()
Now one more scenario "From Activity A start Activity B by calling StartActivity(Intent) on button click and use finish() method inside onstart() method on Activity B"
A onPause(), B onCreate(), B onStart(), A onResume(), B onStop(), B onDestory()
Here's the order of operations that occur when Activity A starts Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.
1
The key is understanding how activity is started. When you publish Intent in startActivity() method you just ask system to start this activity. Next system try to start Activity2 and sends message to Activity1. Order is undetermined and can be different in different situations.
Looks like my anwer was wrong for situation when both activities works within this same process (app)
As pointed Daniil Popov: https://developer.android.com/guide/components/activities/activity-lifecycle.html (Coordinating activities section)
When ever we navigate from first activity to second then
onPause() method is called followed by the onStop() and then the method onCreate() of second activity is called followed by onStart() and then onResume().
Also when navigating back to firstactivity by pressing back key
onPause() method of second activity is called followed by the onStop() and then the method onRestart() of first activity is called followed by onStart() and then onResume().
Use Log to post logs to Logcat.
Log.v("STATE", "Pause...and so on");
The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:
Activity A's onPause() method executes. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.) Then, if Activity A is no longer visible on screen, its onStop() method executes.
I have an App, which uses an ActicityGroup to have a stack of Activitys. If I start a new Activity, I do this as Child of my ActivityGroup. Lets assume I'm in my starting Activity (1), and I start a new one(2), so here is what's getting called:
(1): onPause()
(2): onCreate(), onStart(), onResume()
until here, everything as aspected. if I press my BackButton the stack is as following:
(2): onPause(), onStop(), onDestroy()
(1): onStop(), onDestroy() [sic]
(1): onCreate(), onStart(), onResume()
I see no reason,first why (1) should perform onStop, and onDestroy(), to reCreate again, and second why onRestart never gets called on (1).
Has anyone a Reason for this behavior? Can I 'cancel' calls to onStop() or onDestroy() somehow?
any idea apreciated
Try using FLAG_ACTIVITY_SINGLE_TOP when starting child activity, like:
Window window = getLocalActivityManager().startActivity(id,
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
More info here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
You can avoid Activity onDestroy method when back button pressed in a very simple way,
Just mention
android:persistent="true"
for your activity, that should prevent your activity getting destroyed, To know more in detail you can visit the below SO post
Prevent activity from being destroyed as long as possible
when you press back button it's default behavior is stop or destroy the current activity you can override the back button. If you press home button then it will use the onPause() that means os will consider that you want to continue with current activity when you again launch same activity but if you press back button it means that you finish your current activity and back to the last activity
Onstop() and onDestroy() will not call untill you did not finish activity. Ondestory() called for releasing of resource that occupied by Activity. IF activity use has been over then it is better to destroy this activity.
Second it will be good for memory management scheme. and if you do not call destroy then it will automatically call when you exit from app
and finally
if you doesnot want to call destroy when you press back button then you can use override method onBackpressed()