android奇怪的问题: onActivityResult在onCreate之前就调用了

There's a bug / feature (?) in Android, which immediately reports result (which has not been set yet) for activity, declared as singleTask (despite the fact that the activity continues to run). If we change launchMode of the parent activity from singleTask to singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTask activity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.

当一个activity(A) launchMode设置成singleTask后 前一个activity(B)如果用startActivityForResult来启动这个Activity(B)那么 A的onActivityResult会提前调用

解决办法

启动activity A时设置flag
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);

FLAG_ACTIVITY_CLEAR_TOP

如果在ABCD的堆栈状态下,以该标识启动B,则会销毁CD,且B也是重新创建的(与singleTask有区别),如果配合FLAG_ACTIVITY_SINGLE_TOP,则就会成为singleTask的模式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容