18

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?

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84

9 Answers9

55

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()

Karthikeyan
  • 196
  • 2
  • 10
SAURABH_12
  • 2,262
  • 1
  • 19
  • 19
  • If you press back button from Activity B then lifeCycle call will be...you have not mentioned onSop() before calling onDestroy() for Activity B – Deepak Ganachari Apr 12 '18 at 14:39
  • Suppose in Activity A you make a retrofit call and waiting for the reponse and in response you have a startactivity intent to Activity B. Now the user presses home button, Activiy A onstop gets called and after that retrofit reponse comes since there is an intent to Activity B what will happen now? Will the Activity B open or not? I have seen that it gets opened and come to the foreground without bringing it. It behaves like a error in my Application. What's happening? – Aman Verma Jan 13 '19 at 14:14
  • Why will onDestroy be called for Activity B when you press the back button, but Activity A is not destroyed when you leave it and start Activity B? Is this because you might declare Activity A to be the Main Activity in the Android Manifest file? – Peter G. Williams Dec 21 '19 at 02:30
12

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.

Community
  • 1
  • 1
fedepaol
  • 6,834
  • 3
  • 27
  • 34
  • Android Developer website is updated and the `coordinating activities` section is at https://developer.android.com/guide/components/activities/activity-lifecycle.html – Sungam Feb 28 '17 at 20:37
  • Suppose in Activity A you make a retrofit call and waiting for the reponse and in response you have a startactivity intent to Activity B. Now the user presses home button, Activiy A onstop gets called and after that retrofit reponse comes since there is an intent to Activity B what will happen now? Will the Activity B open or not? I have seen that it gets opened and come to the foreground without bringing it. It behaves like a error in my Application. What's happening? – Aman Verma Jan 13 '19 at 14:15
12

enter image description here

when I have checked it by programmatically its following all steps and easy to understand

Vivek Hande
  • 929
  • 9
  • 11
10

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()
gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
Manju
  • 101
  • 1
  • 3
3

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

Community
  • 1
  • 1
Raghav Bhatia
  • 165
  • 1
  • 5
  • +1 for the image. Here's the link for reference: https://developer.android.com/guide/components/activities/activity-lifecycle#coordinating-activities – h8pathak Jan 09 '20 at 21:19
2

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)

piotrpo
  • 12,398
  • 7
  • 42
  • 58
  • 3
    Order is determined - http://developer.android.com/guide/components/activities.html#CoordinatingActivities – Daniil Popov Oct 28 '14 at 10:33
  • 2
    Android Developer website is updated and the `coordinating activities` section is at https://developer.android.com/guide/components/activities/activity-lifecycle.html – Sungam Feb 28 '17 at 20:36
  • Suppose in Activity A you make a retrofit call and waiting for the reponse and in response you have a startactivity intent to Activity B. Now the user presses home button, Activiy A onstop gets called and after that retrofit reponse comes since there is an intent to Activity B what will happen now? Will the Activity B open or not? I have seen that it gets opened and come to the foreground without bringing it. It behaves like a error in my Application. What's happening? – Aman Verma Jan 13 '19 at 14:15
1

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().

Ishu
  • 5,357
  • 4
  • 16
  • 17
  • I just added some logging code into my app and I'm seeing a different picture: 1) Activity1 onPause() 2) Activity2 onStart 3) activity2 onResume() 4) Activity1 onStop(). – Anton Cherkashyn Dec 19 '13 at 20:07
0

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.

Vijay Antil
  • 51
  • 1
  • 3
0

Use Log to post logs to Logcat.

Log.v("STATE", "Pause...and so on");
Pew Labs
  • 809
  • 2
  • 8
  • 12