Android singleTask或singleInstance与startActivityForResult冲突解决

从Activity A中使用startActivityForResult启动Activity B(Manifest中Activity的声明包含android:launchMode="singleTask"), 但是调用startActivityForResult后马上调用A里面的protected void onActivityResult(int requestCode, int resultCode, Intent data),系统返回resultCode=RESULT_CANCELED。在B中的setResult也没有任何意义了。

Logcat输出如下的信息:

Activity is launching as a new task, so cancelling activity result.

看了startActivityForResult的文档中有下面关键一句:

For example, if the activity you
     * are launching uses the singleTask launch mode, it will not run in your
     * task and thus you will immediately receive a cancel result.


Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as Intent.ACTION_MAIN or Intent.ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result. 

也就是说B不能是单例或单任务启动模式,即launchMode不能这样设置:

android:launchMode= "singleInstance"
android:launchMode="singleTask"

去掉B 中的如上设置,运行正常。

因此,对于singInstance或singleTask的Activity只能采用其他的方式来传递数据。

这两个LaunchMode标识只能用在startActivity()的方法中,而不能使用在startActivityForResult方法中。因为从Task的角度看,Android认为不同Task之间的Activity是不能传递数据的。所以也不会有有用的result返回。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值