美文网首页
Android onActivityResult() resul

Android onActivityResult() resul

作者: 会撒娇的犀犀利 | 来源:发表于2018-07-16 14:25 被阅读0次

    问题

    当 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 模式, 即为singleTasksingleInstance模式时,将立即接口一个cancel result ,即resultCode为0

    • launchModestandardsingleTop
      调用 startActivityForResult() 的Activity launchModestandardsingleTop两个模式其中之一, onActivityResult() 会在Activity中回调,此时 resultCode = -1,也是RESULT_OK;

    • launchModesingleTasksingleInstance
      调用 startActivityForResult() 的Activity launchModesingleTasksingleInstance两个模式其中之一, onActivityResult() 会在Activity中回调,此时 resultCode = 0 ;

    相关文章

      网友评论

          本文标题:Android onActivityResult() resul

          本文链接:https://www.haomeiwen.com/subject/dicbpftx.html