简体   繁体   English

Android活动生命周期:新活动开始时的状态顺序

[英]Android activity lifecycle: state order when new activity starts

If I launch Activity2 from Activity1 by this way: startActivity(Activity2); 如果通过这种方式从Activity1启动Activity2: startActivity(Activity2); what executes first: onStop() (Activity1) or onStart() (Activity2) ? 什么首先执行: onStop() (Activity1)或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通过调用StartActivity(Intent)方法开始活动B,然后生命周期调用如下:-

  • A onCreate() 一个onCreate()
  • A onStart() 一个onStart()
  • A onResume() 一个onResume()

Now Button click for startActivity(intent) 现在按钮单击开始活动(意图)

  • A onPause() 一个onPause()

  • B onCreate() B onCreate()

  • B onStart() B onStart()

  • B onResume() B onResume()

  • A onStop() 一个onStop()

..... If you press back button from Activity B then lifeCycle call will be ..... .....如果您在活动B中按返回按钮,则生命周期调用将是.....

  • B onPause() B onPause()

  • A onRestart() 一个onRestart()

  • A onStart() 一个onStart()

  • A onResume() 一个onResume()

  • B onStop() B onStop()
  • B onDestory() 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通过单击按钮调用StartActivity(Intent)并在活动B的onstart()方法内部使用finish()方法来启动活动B”

  • A onPause() 一个onPause()

  • B onCreate() B onCreate()

  • B onStart() B onStart()

  • A onResume() 一个onResume()

  • B onStop() B onStop()

  • B onDestory() 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). 根据文档,在Activity1上的onStop之前会调用Activity2上的onStart(或者,如果您愿意,操作系统会在Activity1上调用onStop之前先等待Activity2上的onStart完成)。

From http://developer.android.com/guide/topics/fundamentals/activities.html : 来自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: 这是活动A启动活动性B时发生的操作顺序:

Activity A's onPause() method executes. 活动A的onPause()方法执行。 Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. 活动B的onCreate(),onStart()和onResume()方法按顺序执行。 (Activity B now has user focus.) Then, if Activity A is no longer visible on screen, its onStop() method executes. (现在,活动B具有用户焦点。)然后,如果活动A在屏幕上不再可见,则执行其onStop()方法。

The key is understanding how activity is started. 关键是要了解活动是如何开始的。 When you publish Intent in startActivity() method you just ask system to start this activity. 在startActivity()方法中发布Intent时,您只是要求系统启动此活动。 Next system try to start Activity2 and sends message to Activity1. 下一个系统尝试启动Activity2并将消息发送到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) 当两个活动都在相同的流程(应用程序)中工作时,我似乎无所适从。如所指出的,丹尼尔·波波夫(Daniil Popov): https : //developer.android.com/guide/components/activities/activity-lifecycle.html (协调活动部分)

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(). 每当我们从第一个活动导航到第二个活动时,均会先调用onPause()方法,再调用onStop(),然后再调用第二个活动的onCreate()方法,再调用onStart(),然后再调用onResume()。

Also when navigating back to firstactivity by pressing back key 同样在按返回键导航回firstactivity时

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(). 调用第二个活动的onPause()方法,然后调用onStop(),然后调用第一个活动的onRestart()方法,然后调用onStart(),然后调用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: 这是活动A启动活动性B时发生的操作顺序:

Activity A's onPause() method executes. 活动A的onPause()方法执行。 Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. 活动B的onCreate(),onStart()和onResume()方法按顺序执行。 (Activity B now has user focus.) Then, if Activity A is no longer visible on screen, its onStop() method executes. (现在,活动B具有用户焦点。)然后,如果活动A在屏幕上不再可见,则执行其onStop()方法。

Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this:- 假设活动A通过Calling StartActivity(Intent)方法启动活动B ,然后生命周期调用如下:

  • A onCreate() , A onStart() , A onResume() A onCreate()A onStart()A onResume()

Now Button click for startActivity(intent) 现在按钮单击开始活动startActivity(intent)

  • A onPause() , B onCreate() , B onStart() , B onResume() , A onStop() A onPause()B onCreate()B onStart()B onResume()A onStop()

If you press back button from Activity B then lifeCycle call will be ..... 如果您在活动B按返回按钮,则生命周期调用将为.....

  • B onPause() , A onRestart() , A onStart() , A onResume() , B onStop() , B onDestory() 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开始活动B致电StartActivity(Intent)按钮点击使用finish()内法onstart()方法上的活动B

  • A onPause() , B onCreate() , B onStart() , A onResume() , B onStop() , B onDestory() 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: 这是活动A启动活动B时发生的操作顺序:

Activity A's onPause() method executes. 活动A的onPause()方法执行。

Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. 活动B的onCreate(),onStart()和onResume()方法按顺序执行。 (Activity B now has user focus.) (活动B现在具有用户焦点。)

Then, if Activity A is no longer visible on screen, its onStop() method executes. 然后,如果活动A在屏幕上不再可见,则执行其onStop()方法。

1 1个

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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