前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >添加Intent.FLAG_ACTIVITY_CLEAR_TOP 都做了些什么背景整体流程

添加Intent.FLAG_ACTIVITY_CLEAR_TOP 都做了些什么背景整体流程

作者头像
用户1127566
发布2018-06-06 16:13:51
6640
发布2018-06-06 16:13:51
举报
文章被收录于专栏:Android 开发学习Android 开发学习

背景

最近老是看到各种面试中的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
代码语言:javascript
复制
 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。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 整体流程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档