onActivityResult and launch mode

2015-03-31  本文已影响191人  lguipeng

A Activity想通过startActivityForResult()启动 B Activity,B的launch mode是singleTask,A中的onActivityResult()会马上被调用,并且resultCode为0(RESULT_CANCELED),也就是说根本不是我们期望的。

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.(摘选自Android官方文档)

最重要的意思是说如果使用了singleTask ,onActivityResult 会马上返回,而且返回值是cancel result,而原因就是新启动的Activity没有在你的task里面跑,因为singleTask会创建新的task。所以就导致了上面的问题。

既然是launch mode导致的问题,只需要把mainfest文件中把B Activity的
“launchMode”改为“standard”或者“singleTop”就可以了

上一篇下一篇

猜你喜欢

热点阅读