美文网首页
添加Intent.FLAG_ACTIVITY_CLEAR_TOP

添加Intent.FLAG_ACTIVITY_CLEAR_TOP

作者: nothingwxq | 来源:发表于2017-08-16 20:01 被阅读98次

背景

最近老是看到各种面试中的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。

相关文章

网友评论

      本文标题:添加Intent.FLAG_ACTIVITY_CLEAR_TOP

      本文链接:https://www.haomeiwen.com/subject/uijprxtx.html