简体   繁体   中英

Android activity lifecycle: state order when new activity starts

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.

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

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.

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

使用日志将日志发布到Logcat。

Log.v("STATE", "Pause...and so on");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM