问题
当 Activity调用startActivityForResult()方法后 ,回调函数 onActivityResult()中参数 resultCode 等于0?
原因
主要原因是跟Activity的 launchMode启动方式有关:
/**
* 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).
*
* <p>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.
*
* <p>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.
*
* <p>This method throws {@link android.content.ActivityNotFoundException}
* if there was no Activity found to run the given Intent.
*
* @param intent The intent to start.
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)}
* Context.startActivity(Intent, Bundle)} for more details.
*
* @throws android.content.ActivityNotFoundException
*
* @see #startActivity
*/
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {}
在上面注释中有这么一句话:
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.
意思是:如果你的Activity为FLAG_ACTIVITY_NEW_TASK 模式, 即为singleTask或singleInstance模式时,将立即接口一个cancel result,即resultCode为0
-
launchMode为standard或singleTop
调用startActivityForResult()的ActivitylaunchMode为standard、singleTop两个模式其中之一,onActivityResult()会在Activity中回调,此时resultCode = -1,也是RESULT_OK; -
launchMode为singleTask或singleInstance
调用startActivityForResult()的ActivitylaunchMode为singleTask、singleInstance两个模式其中之一,onActivityResult()会在Activity中回调,此时resultCode = 0;







网友评论