Activity之间返回结果

来源:互联网 发布:usb端口检测软件 编辑:程序博客网 时间:2024/04/26 03:32

一 、实现:

1、在MainActivity中:

1)onCreate()中:

Intent intent = new Intent(MainActivity.this,OtherActivity.class);startActivityForResult(intent,0); //startActivityForResult(Intent intent,int requestcode)

2)重写onActivityResult():

@overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //处理返回的数据        String name = data.getBundleExtra("data").getString("name");        message.setText(name);    }

2、在OtherActivity中:

1)onCreate()中:

Intent intent = this.getIntent();Bundle bundle = new Bundle();bundle.put("name","Jack");intent.putExtra("BundleKey",bundle);setResult(0,intent); //setResult(int resultCode, Intent intent);

二 、过程:

1)首先,来看startActivityForResult(intent,0);

进入FragmentActivity.java:

/**     * Modifies the standard behavior to allow results to be delivered to fragments.     * This imposes a restriction that requestCode be <= 0xffff.     */    @Override    public void startActivityForResult(Intent intent, int requestCode) {        if (requestCode != -1 && (requestCode&0xffff0000) != 0) {            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");        }        super.startActivityForResult(intent, requestCode);    }

判断requestCode是否小于16 bits,

如果requestCode <= 16bits,再进入Activity.java:

/**     * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}     * with no options.     *     * @param intent The intent to start.     * @param requestCode If >= 0, this code will be returned in     *                    onActivityResult() when the activity exits.     *     * @throws android.content.ActivityNotFoundException     *     * @see #startActivity     */    public void startActivityForResult(Intent intent, int requestCode) {        startActivityForResult(intent, requestCode, null);    }    /**     * @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(Intent intent, int requestCode, @Nullable Bundle options) {        if (mParent == null) {            Instrumentation.ActivityResult ar =                mInstrumentation.execStartActivity(                    this, mMainThread.getApplicationThread(), mToken, this,                    intent, requestCode, options);            if (ar != null) {                mMainThread.sendActivityResult(                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),                    ar.getResultData());            }            if (requestCode >= 0) {                              mStartedActivity = true;            }            cancelInputsAndStartExitTransition(options);            // TODO Consider clearing/flushing other event sources and events for child windows.        } else {            if (options != null) {                mParent.startActivityFromChild(this, intent, requestCode, options);            } else {                // Note we want to go through this method for compatibility with                // existing applications that may have overridden it.                mParent.startActivityFromChild(this, intent, requestCode);            }        }    }

判断requestCode是否大于0,
If requestCode >= 0, this code will be returned in onActivityResult() when the activity exits.

2)然后,来看setResult(0,intent);

进入Activity.java:

/**     * Call this to set the result that your activity will return to its     * caller.     *     * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent     * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION     * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION     * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set.  This will grant the     * Activity receiving the result access to the specific URIs in the Intent.     * Access will remain until the Activity has finished (it will remain across the hosting     * process being killed and other temporary destruction) and will be added     * to any existing set of URI permissions it already holds.     *     * @param resultCode The result code to propagate back to the originating     *                   activity, often RESULT_CANCELED or RESULT_OK     * @param data The data to propagate back to the originating activity.     *     * @see #RESULT_CANCELED     * @see #RESULT_OK     * @see #RESULT_FIRST_USER     * @see #setResult(int)     */    public final void setResult(int resultCode, Intent data) {        synchronized (this) {            mResultCode = resultCode;            mResultData = data;        }    }

把resultCode,data返回给MainActivity的onActivityResult(int requestCode, int resultCode, Intent data)

3)@override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //处理返回的数据    }

明白其中的原理,学起来会很快,加油!

0 0
原创粉丝点击