-1

when I use startActivityForResult() to start Activity2 in Activity1, if Activity1 and Activity2 are not in the same task stack, the resultCode is 0(Activity.RESULT_CANCELED).

IF Activity1's launchMode is standard and Activity2's launchMode is singleTask, the resultCode I got is 0.

What happened in stack?

If Activity1's launchMode is singleTop and Activity1 use the startActivityForResult() method to start itself, the stack has two instances of Activity1.

What happened in stack?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93

1 Answers1

0

I got the answers myself.

For the first question,I found the answer in the note of method startActivityForResult(Intent,int,Bundle).

enter image description here

As shown in the following figure,the shown Activity may be ActivityX if ActivityY finishes ,so it immediately receive a cancel result for the uncertain result.It just happens when api is below 20.

enter image description here

For the next question,I found the answer in source code.

ActivityStack topStack = getFocusedStack(); ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop); if (top != null && r.resultTo == null) { Slog.d("DDY", "========------ " ); if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) { if (top.app != null && top.app.thread != null) { if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { ...... top.deliverNewIntentLocked(callingUid, r.intent);

When a Activity try to start itself,r.resultTo is not null,so it start with standard mode.