StartActivityForResult启动模式在不同场景下的不同结果

这里写图片描述

StartActivityForResult启动模式在不同场景下的不同结果_第1张图片
在4.4 singleTask加taskaffinity也能新建任务栈

StartActivityForResult启动模式在不同场景下的不同结果_第2张图片
4.4,Main3设置singleInstance,则建立新的任务栈

一,使用startActivityForResult
在4.4:
1.启动一个正常的LaunchMode是standard的activity ,可以接收到resultOK
2.启动一个有NEW_TASK的flag的activity(如设置singleTask(即使没设置affnity,和启动activity在同一个栈中也是不能成功),或者singleInstance的,都不能正常接收的result.返回result_Cancel,在启动另一个Activity的时候会立即返回一个Result_Cancel,且空intent)
PS:这时候的newTask标签(singleTask设置taskAffnity或singleInstance模式)能正常打开新的task栈
在5.0以上手机:
1.只要是通过startActivityForResult启动的activity,不管启动的activity的flag属性及launchMode,一律作为sub_activity,放在启动者当前的任务栈中,接受到的result是成功的
2.启动者的模式即使是singleInstance,被启动的activity仍然会放到同一个栈中。
二,使用startActivity即使用startActivityForResult(requestCode = -1,认为是普通的startActivity)
1.在4.4:
表现均一样。
2.在5.0+:

startActivityForResult的源码注释

说明了问题,requestCode >=0 表示是需要返回结果的,要启动的Activity会作为sub_activity添加到你的栈中,否则认为是普通的startActivity

/**
     * Launch an activity for which you would like a result when it finished.
     * When this activity exits, your
     * onActivityResult() method will be called with the given requestCode.
     * Using a negative requestCode is the same as calling
     * {@link #startActivity} (the activity is not launched as a sub-activity).
     *
     * 

Note that this method should only be used with Intent protocols * that are defined to return a result. In other protocols (such as * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may * not get the result when you expect. For example, if the activity you * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, 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. *

你可能感兴趣的:(Android)