background
Recently, I've been seeing lanchMode and Intent Flag in various interviews. Most of the analysis stays on the surface, and some of them are self-contradictory. lz recently
To make a requirement, we need to use Intent flag, consult the relevant information below, and prove the reason from the source code. Add Intent.FLAG_ACTIVITY_CLEAR_TOP. Let's see how the system works. Don't ask me how to find the source code. I won't tell you it was found through Android xref.
Whole process
The whole logic of private start activity unchecked is in this function, and the logic is clear.
-
setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession, voiceInteractor);
Initialize lauchMode and Intent Flags -
computeLaunchingTaskFlags();
-
computeSourceStack();
-
MReusedActivity = getReusable IntentActivity (); Find reusable activities
-
Special Flag Processing such as FLAG_ACTIVITY_CLEAR_TOP in this article
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
1025 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1026 int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask) {
1027
1028 setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
1029 voiceInteractor);
1030
1031 computeLaunchingTaskFlags();
1032
1033 computeSourceStack();
1034
1035 mIntent.setFlags(mLaunchFlags);
1036
1037 mReusedActivity = getReusableIntentActivity();
1038
1039 final int preferredLaunchStackId =
1040 (mOptions != null) ? mOptions.getLaunchStackId() : INVALID_STACK_ID;
1041
1042 if (mReusedActivity != null) {
......
1066 if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
1067 || mLaunchSingleInstance || mLaunchSingleTask) {
1068 // In this situation we want to remove all activities from the task up to the one
1069 // being started. In most cases this means we are resetting the task to its initial
1070 // state.
1071 final ActivityRecord top = mReusedActivity.task.performClearTaskForReuseLocked(
1072 mStartActivity, mLaunchFlags);
1073 if (top != null) {
1074 if (top.frontOfTask) {
1075 // Activity aliases may mean we use different intents for the top activity,
1076 // so make sure the task now has the identity of the new intent.
1077 top.task.setIntent(mStartActivity);
1078 }
1079 ActivityStack.logStartActivity(AM_NEW_INTENT, mStartActivity, top.task);
1080 top.deliverNewIntentLocked(mCallingUid, mStartActivity.intent,
1081 mStartActivity.launchedFromPackage);
1082 }
1083 }
1084
....... //Why omit in the middle? Intent.FLAG_ACTIVITY_CLEAR_TOP, which is not related to our topic discussion, has little correlation with other flag processing.
1250 }
Go straight to Step 4

The annotations are very detailed, let alone deal with lanchMode and flag
Step 5 Processing FLAG_ACTIVITY_CLEAR_TOP

The main task is to find the corresponding activity record, traverse from back to front, simulate the stack operation, and then find the target activity, and remove the activity finish es on the top of the activity stack. Finally, deal with Activity. finish the activity if there is no intent flag for singletop.