关于startActivityForResult问题

      今天把startActivity换成了更好用的startActivityForResult,结果发现跟预期的效果有点出入。在A中执行了startActivityForResult方法跳转到B后,未等执行B中的setResult方法,立刻执行了A中重写的onActivityResult方法。而且在B中执行了setResult方法后,A中的onActivityResult方法中返回的resultCode每次都是RESULT_CANCELED,而不是setResult(int resultCode)里的参数。

      翻文档发现startActivityForResult方法里有个Note

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or 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.

As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity.

这里主要是对A的android:launchMode作了要求,该属性可以在 AndroidManifest.xml 中设置,设置为默认的启动模式就行了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值