1

As we can know, the startActivity method will be invoked startActivityForResult method; And some code like as below:

    /**
 * 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(@RequiresPermission Intent intent, int requestCode) {
    startActivityForResult(intent, requestCode, null);
}

I try my best to find the source code where deal the requestCode. But I found nothing. Could somebody can copy the source code and tell me how it deal with when only requestCode>=0the OnActivityResult() invoked?

stkent
  • 19,772
  • 14
  • 85
  • 111
Longalei
  • 453
  • 3
  • 8
  • please visit [this](https://stackoverflow.com/questions/25529865/java-lang-illegalargumentexception-can-only-use-lower-16-bits-for-requestcode) link.here it is explained. – Amit Goswami Mar 22 '19 at 12:03

1 Answers1

1

As shown in the code snippet you included, the two-parameter version of startActivityForResult calls into a three-parameter version of startActivityForResult. The three-parameter version looks like this:

public void startActivityForResult(@RequiresPermission 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) {
            // If this start is requesting a result, we can avoid making
            // the activity visible until the result is received.  Setting
            // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
            // activity hidden during this time, to avoid flickering.
            // This can only be done when a result is requested because
            // that guarantees we will get information back when the
            // activity is finished, no matter what happens to it.
            mStartedActivity = true;
        }

        //...
    } else {
        //...
    }
}

The comment in the if (requestCode >= 0) {} block describes how behavior changes when the request code is >= 0 vs < 0.

If we look at the source code for mInstrumentation.execStartActivity, we find the following:

public ActivityResult execStartActivity(
        Context who, IBinder contextThread, IBinder token, Activity target,
        Intent intent, int requestCode, Bundle options) {

    //...

                    if (am.isBlocking()) {
                        return requestCode >= 0 ? am.getResult() : null;
                    }

    // ...
}

The line return requestCode >= 0 ? am.getResult() : null; returns a null ActivityResult if the requestCode is negative. Jumping back to the startActivityForResult code at the top of this answer, we see that mMainThread.sendActivityResult is called if and only if the ActivityResult returned by execStartActivity is non-null. It is sendActivityResult that eventually results in a call to onActivityResult. So the path of logic here is:

startActivityForResult is called with a negative requestCode
mInstrumentation.execStartActivity is called with a negative requestCode
mInstrumentation.execStartActivity returns null
mMainThread.sendActivityResult is NOT called
onActivityResult is NOT called

stkent
  • 19,772
  • 14
  • 85
  • 111