关于Activity onNewIntent方法的调用时机

版权声明:本文为博主原创文章,未经博主允许不得转载。https://mp.csdn.net/postedit/78799540

在官方API上的说明如下:

http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent):

protected void onNewIntent (Intent intent)

Since: API Level 1

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when callingstartActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Parameters

intent The new intent that was started for the activity.

See Also

Activity 的 onNewIntent方法的调用可总结如下:

  在该Activity的实例已经存在于Task和Back stack中(或者通俗的说可以通过按返回键返回到该Activity )时,当使用intent来再次启动该Activity的时候,如果此次启动不创建该Activity的新实例,则系统会调用原有实例的onNewIntent()方法来处理此intent.

  且在下面情况下系统不会创建该Activity的新实例:

  1,如果该Activity在Manifest中的android:launchMode定义singleTask或者singleInstance.

  2,如果该Activity在Manifest中的android:launchMode定义singleTop且该实例位于Back stack的栈顶.

  3,如果该Activity在Manifest中的android:launchMode定义singleTop,且上述intent包含Intent.FLAG_ACTIVITY_CLEAR_TOP标志.

  4,如果上述intent中包含 Intent.FLAG_ACTIVITY_CLEAR_TOP 标志和且包含 Intent.FLAG_ACTIVITY_SINGLE_TOP 标志.

  5,如果上述intent中包含 Intent.FLAG_ACTIVITY_SINGLE_TOP 标志且该实例位于Back stack的栈顶.

  上述情况满足其一,则系统将不会创建该Activity的新实例.

  根据现有实例所处的状态不同onNewIntent()方法的调用时机也不同,总的说如果系统调用onNewIntent()方法则系统会在onResume()方法执行之前调用它.这也是官方API为什么只说"you can count on onResume() being called after this method",而不具体说明调用时机的原因.

  下面是不同状态下调用onNewIntent() 的日志以供参考:

  1,如果实例已经被系统kill掉:    

10-23 14:03:21.623: D/MainActivity(25990): onCreate
10-23 14:03:21.743: D/MainActivity(25990): onStart
10-23 14:03:21.743: D/MainActivity(25990): onRestoreInstanceState
10-23 14:03:21.743: D/MainActivity(25990): onNewIntent
10-23 14:03:21.753: D/MainActivity(25990): onResume

  2,如果实例已被stop:

10-23 12:08:32.063: D/MainActivity(15188): onNewIntent
10-23 12:08:32.063: D/MainActivity(15188): onRestart
10-23 12:08:32.063: D/MainActivity(15188): onStart
10-23 12:08:32.063: D/MainActivity(15188): onResume

  3,如果实例已被pause:

10-23 13:47:08.393: D/MainActivity(25672): onNewIntent
10-23 13:47:08.393: D/MainActivity(25672): onResume

  第2,3可以总结为如果实例没有被Kill则会首先执行onNewIntent方法,然后再执行生命周期的其他方法.

文章出自: https://www.cnblogs.com/zawn/archive/2012/10/23/2735875.html

猜你喜欢

转载自blog.csdn.net/qq_32425789/article/details/78799540
今日推荐