2个Activity先后跳转到同一个Activity,后跳转的Activity,传值到目标Activity时失败

来源:互联网 发布:人工智能芯片概念股 编辑:程序博客网 时间:2024/05/29 18:58


一: 问题描述

有3个Activity,A、C、D,D是目标Activity。  A先跳转到D,然后C跳转到D,跳转以及传递数据都是通过发送Intent。

问题是:在D中接收到的数据始终是A传递到D中的数据,接收不到C传递过来的数据。

二:解决方法

onNewIntent(Intent intent);  此方法属于Activity类,功能: 不知该怎么说,提炼不出来,网上的提炼感觉不怎么好,提炼出来的网友可以给我留言。

解决方法参考:http://www.cnblogs.com/zenfly/archive/2012/02/10/2345196.html

提取解决思路:(1)当D第一次被A启动时,因为是第一次启动D,所以会执行onCreate方法,但并不会执行onNewIntent(Intent intent)方法,此时D的实例已存在;为了保证只有D的实例只有一个signTask;

(2)然后C启动D时,执行onNewIntent(Intent intent);

(3)  当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getIntent()都是得到老的Intent,即A中传过来的intent。

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

setIntent(intent);//must store the new intent unless getIntent() will return the old one

processExtraData();

}





0 0
原创粉丝点击