android 开发 singleTask启动模式下传值的坑

日常填坑。


做了什么操作引起的?如下: 

在活动A 启动模式设置为singleTask ,然后再用活动A启动活动B,活动B启动活动C。 现在我的活动C要使用intent携带值去启动活动A。在活动A中正常的操作去获取值,会发现intent中没有任何值。


为什么会这样呢?如下:

因为活动A里的intent是旧的,之前活动A启动就已经保留的intent。如果这个时候用getIntent方法是无法获取活动C携带值的Intent。


如何解决?如下:

我们需要更新活动A里的intent,把原先的intent重新覆盖活动C传过来的intent。


具体操作:

    @Override
    protected void onNewIntent(Intent intent) {
        Log.e(TAG, "onNewIntent被调用");
        super.onNewIntent(intent);
        setIntent(intent);
    }
在活动A里重写一个 onNewIntent方法,让活动A在恢复活动的时候更新到最新的intent。

猜你喜欢

转载自blog.csdn.net/qq_37217804/article/details/80665884
今日推荐