添加Intent.FLAG_ACTIVITY_CLEAR_TOP 都做了些什么

背景

最近老是看到各種面試中的lanchMode和Intent Flag, 大多數分析停留在表面, 并且有些還自相矛盾。lz 最近
做一個需求需要用Intent flag, 查閱了下相關資料, 并從源碼論證了原因。添加Intent.FLAG_ACTIVITY_CLEAR_TOP我們來看看系統是怎么做的? 別問我怎么找到源碼的,我不告訴你是通過androidxref查找的。

整體流程

private int startActivityUnchecked 整體的邏輯就在這個函數中了,邏輯也比較清楚,

  1. setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession, voiceInteractor);
    初始化lauchMode和Intent Flags

  2. computeLaunchingTaskFlags();

  3. computeSourceStack();

  4. mReusedActivity = getReusableIntentActivity(); 查找可復用的activity

  5. 特殊Flag處理如本文的 FLAG_ACTIVITY_CLEAR_TOP

 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
 .......  //中間為嘛省略呢? 與我們主題討論的Intent.FLAG_ACTIVITY_CLEAR_TOP 相關度不大,其他的flag處理
1250    }

直接到第4步

image.png

注釋很詳細,不多說了,處理lanchMode和flag

第5步 處理FLAG_ACTIVITY_CLEAR_TOP

image.png

主要是查找到對應的activity記錄,從后往前遍歷,模擬棧操作,然后找到目標activity, 將該activity棧頂的activity finish掉。 最后處理Activity, 如果不含有singletop 的intent flag 就finish掉該activity。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容