Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android的任务和返回堆栈 #23

Open
soapgu opened this issue Mar 29, 2021 · 0 comments
Open

Android的任务和返回堆栈 #23

soapgu opened this issue Mar 29, 2021 · 0 comments
Labels
安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Mar 29, 2021

讲实话,写这篇博客的同时对Android的多任务和堆栈的原理还是一知半解,还需要一段时间的实践加深理解
关于这个Topic其实以前在学习Activity 是看过的。看过以后其实状态时这样的
图片

这次选择杀一个回马枪,确实比当时的理解要高了一个层次了

定义启动模式

  • Manifest中定义

顾名思义是在Manifest配置中声明,所以定义是固定的(静态的)

  • Intent 中定义

往往是伴随着startActivity()代码调用,可以认为是程序控制的

这里注意下优先级
如果两个 Activity 都定义了 Activity B 应如何与任务关联,将优先遵循 Activity A 的请求(在 intent 中定义),而不是 Activity B 的请求(在清单中定义)。

launchMode的4S(4个儿子)

4个儿子分别是standard,singleTop,singleTask,singleInstance
这个命名就是故意的,我很想让设计这4个mode的程序员认一下“己、已、巳”这三个字分别是啥意思。

  • standard

大儿子属于默认模式,不背书了。理解下来就是没有限制对象的数量,而归属到任务的限制,就是有需要就new一个。

  • singleTop

二儿子比大儿子稍微有规矩一点,就是只要请求的Activity是当前的Activity,不重复创建,保持当前Activity

  • singleTask

从三儿子开始就开始头疼了。就是说这个Activity归属一个独立的Task。但是这个Task本身可以有其他的Activity,但是启动他会把这个Task的堆栈一起带过去

  • singleInstance

四儿子和三儿子有点傻傻的分不清楚。三儿子四儿子放一起就懵圈了,StackOverflow有相对比较带感一点的解释

The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

Difference between singleTask and singleInstance

从自己理解角度来解释,就是singleInstance这个4儿子就是一个天煞孤星,他从来不和其他的activity 待一起(task是专属的)。永远也只有一个activity存在。

Intent 标记的3F(3个Flag)

  • FLAG_ACTIVITY_NEW_TASK

    = singleTop =三儿子
  • FLAG_ACTIVITY_SINGLE_TOP

= singleInstance = 四儿子

  • FLAG_ACTIVITY_CLEAR_TOP

这个标记有一点特殊,首先他保持着只有一个activity 特性。有个特性,如果activity存在,但是会把这个activity前面的堆栈全清理干净

Clearing the back stack

清理堆栈的三个属性

  • alwaysRetainTaskState

作用域整个任务,堆栈会一直保持

  • clearTaskOnLaunch

作用域整个任务,只要离开堆栈就会被清理,只剩root activity。

  • finishOnTaskLaunch

作用域到单个activity,只要离开就会被清出堆栈。永远不会被暂存

@soapgu soapgu added the 安卓 安卓 label Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant