Reputation: 12293
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?
Upvotes: 19
Views: 38540
Reputation: 999
when I have checked it by programmatically its following all steps and easy to understand
Upvotes: 14
Reputation: 205
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.
Upvotes: 3
Reputation: 111
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()
Upvotes: 11
Reputation: 2300
Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this :-
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()
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()
Upvotes: 58
Reputation: 61
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.
Upvotes: 0
Reputation: 12636
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)
Upvotes: 2
Reputation: 6862
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.
Upvotes: 12
Reputation: 5457
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().
Upvotes: 1
Reputation: 809
Use Log to post logs to Logcat.
Log.v("STATE", "Pause...and so on");
Upvotes: 0