美文网首页
第四周——关于startActivityForResult和on

第四周——关于startActivityForResult和on

作者: 阳光的nick_lxz | 来源:发表于2018-07-24 11:05 被阅读0次

    一使用场景

    当我们需要从跳转的Activity中返回时候执行相应的操作。启动的时候就使用startActivityForResult(@RequiresPermission Intent intent, int requestCode)来启动。返回的时候就会触发onActivityResult(int requestCode, int resultCode, Intent data)方法

    二requestCode和resultCode的作用(onActivityResult)

    requestCode:用于标识一个请求,在onActivityResult区分这个请求是哪个页面返回的
    resultCode:用于表示返回值,一般使用(RESULT_OK、RESULT_FIRST_USER、RESULT_CANCELED),我一般都是区别是不是RESULT_OK,是的话就执行返回requestCode对应的操作,否则不执行,这里没有什么具体的限定,随心。
    eg:

                    Intent intent=new Intent(MainActivity.this,Main3Activity.class);
                    startActivityForResult(intent,1);
                    Intent intent2=new Intent(MainActivity.this,Main2Activity.class);
                    startActivityForResult(intent2,2);
    
     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                }
            }
        }
    

    如上,逻辑简单的话,resultCode 用来判断是否要执行操作,requestCode来执行那个操作。复杂的话可以这样

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           super.onActivityResult(requestCode, resultCode, data);
           switch (requestCode) {
               case 1:
                   switch (requestCode) {
                       case 1:
                           break;
                       case 2:
                           break;
                       default:
    
                   }
                   break;
               case 2:
                   break;
               default:
           }
       }
    

    三setResult

    setResult有两个方法

        public final void setResult(int resultCode, Intent data) {
            synchronized (this) {
                mResultCode = resultCode;
                mResultData = data;
            }
        }
    
       /**
         * Call this to set the result that your activity will return to its
         * caller.
         *
         * @param resultCode The result code to propagate back to the originating
         *                   activity, often RESULT_CANCELED or RESULT_OK
         *
         * @see #RESULT_CANCELED
         * @see #RESULT_OK
         * @see #RESULT_FIRST_USER
         * @see #setResult(int, Intent)
         */
        public final void setResult(int resultCode) {
            synchronized (this) {
                mResultCode = resultCode;
                mResultData = null;
            }
        }
    

    通过源码可知就是赋值,而且是给成员变量赋值。这两个成员变量在Activity finish的时候会用到。下面我们看下finish源码

    finish源码

    private void finish(int finishTask) {
            if (mParent == null) {
                int resultCode;
                Intent resultData;
                synchronized (this) {
                //注意这里
                    resultCode = mResultCode;
                    resultData = mResultData;
                }
                if (false) Log.v(TAG, "Finishing self: token=" + mToken);
                try {
                    if (resultData != null) {
                        resultData.prepareToLeaveProcess(this);
                    }
                    if (ActivityManager.getService()
                            .finishActivity(mToken, resultCode, resultData, finishTask)) {
                        mFinished = true;
                    }
                } catch (RemoteException e) {
                    // Empty
                }
            } else {
                mParent.finishFromChild(this);
            }
    
            // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
            // be restored now.
            if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) {
                getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_RESTORE,
                        mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN));
            }
        }
    

    finish取了mResultCode和mResultData这两个成员变量的值,结合setResult源码,所以setResult方法必须在finish之前调用,否则resultCode会是默认值0,mResultData会为NULL。

    相关文章

      网友评论

          本文标题:第四周——关于startActivityForResult和on

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