Activity onNewIntent方法的调用时机

2023-05-16

首先看下官方的API说明:

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.

根据以上说明总结下:
  1. 该Activity的实例必须存在于Task或者Back Stack中
  2. 使用Intent再次启动该Activity,如果此次启动不创建该Activity的新实例,则系统会调用原有实例的onNewIntent()来处理Intent。
  3. onNewIntent()调用时机总是在onResume()方法之前。
满足条件归纳:
  1. Activity在Manifest中的android:launchMode定义为singleTask或者singleInstance。
  2. Activity在Manifest中的android:launchMode定义为singleTop,且该实例处于Back Stack的栈顶。
  3. Activity在Manifest中的android:launchMode定义为singleTop,且intent添加标识为:intent.setFlag(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的栈顶。
一些情境下该函数的调用时机解析
  1. 如果实例已经被系统kill掉:

onCreate
onStart
onRestoreInstanceState
onNewIntent
onResume

  1. 如果实例已被stop:

onNewIntent
onRestart
onStart
onResume

  1. 如果实例已被pause:

onNewIntent
onResume

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Activity onNewIntent方法的调用时机 的相关文章

随机推荐