启动一个启动模式为singleTop的activity

本文详细解释了Android中Activity的onNewIntent(Intent intent)方法的工作原理。此方法会在Activity启动模式为singleTop或通过Intent.FLAG_ACTIVITY_SINGLE_TOP标志启动时被调用。当Activity位于栈顶并接收新的Intent时,该方法将被触发而不是创建新的Activity实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activity 有一个 onNewIntent(Intent intent)回调方法

This is called for activities that set launchMode to “singleTop” in their package, or if a client usedthe Intent.FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity. In either case, when the activityis re-launched while at the top of the activity stack instead of a new instance of the activity beingstarted, onNewIntent() will be called on the existing instance with the Intent that was used to re-launchit.An activity will always be paused before receiving a new intent, so you can count on onResume being calledafter this method.Note that getIntent still returns the original Intent. You can use setIntent to update it to this newIntent.

上文大概意思如下:
该方法被启动模式设置为“singleTop”的 Activity 回调,或者当通过设置 Intent.FLAG_ACTIVITY_SINGLE_TOP的 Intent 启动 Activity 时被回调。 在任何情况下, 只要当栈顶的 Activity 被重新启动时没有重新创建一个新的 Activity实例而是依然使用该 Activity 对象,那么 onNewIntent(Intent)方法就会被回调。当一个 Activity 接收到新 Intent 的时候会处于暂停状态,因此你可以统计到 onResume()方法会被再次执行,当然这个执行是在 onNewIntent 之后的。注意:如果我们在 Activity 中调用了 getIntent()方法,那么返回的 Intent 对象还是老的 Intent(也就是第一次启动该 Activity 时的传入的 Intent 对象),但是如果想让 getIntent()返回最新的 Intent,那么我们可以通过setIntent(Intent)方法设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值