Activity onNewIntent方法的调用时机

首先看下官方的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值