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