参考链接:
https://developer.android.com/guide/components/activities/tasks-and-back-stack
https://developer.android.com/guide/topics/manifest/activity-element
https://blog.youkuaiyun.com/zjwfan/article/details/52047719
官方原文是这么说的:
"singleTask"
The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent()
method, rather than creating a new instance. Only one instance of the activity can exist at a time.
官方说 会 create a new task 然后,实验下来,并没有create a new task。
Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
"singleInstance"
.
Same as "singleTask"
, except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.
意思是说在singleInstance下,栈内只能有一个activity
根据第二个链接,摘录下面官方的文档
“singleTask
”和“singleInstance
”模式同样只在一个方面有差异: “singleTask
”Activity 允许其他 Activity 成为其任务的组成部分。 它始终位于其任务的根位置,但其他 Activity(必然是“standard
”和“singleTop
”Activity)可以启动到该任务中。 相反,“singleInstance
”Activity 则不允许其他 Activity 成为其任务的组成部分。它是任务中唯一的 Activity。 如果它启动另一个 Activity,系统会将该 Activity 分配给其他任务 — 就好像 Intent 中包含 FLAG_ACTIVITY_NEW_TASK
一样。
用例 | 启动模式 | 多个实例? | 注释 |
---|---|---|---|
大多数 Activity 的正常启动 | “standard ” | 是 | 默认值。系统始终会在目标任务中创建新的 Activity 实例并向其传送 Intent。 |
“singleTop ” | 有条件 | 如果目标任务的顶部已存在一个 Activity 实例,则系统会通过调用该实例的 onNewIntent() 方法向其传送 Intent,而不是创建新的 Activity 实例。 | |
专用启动 (不建议用作常规用途) | “singleTask ” | 否 | 系统在新任务的根位置创建 Activity 并向其传送 Intent。 不过,如果已存在一个 Activity 实例,则系统会通过调用该实例的 onNewIntent() 方法向其传送 Intent,而不是创建新的 Activity 实例。 |
“singleInstance ” | 否 | 与“singleTask" ”相同,只是系统不会将任何其他 Activity 启动到包含实例的任务中。 该 Activity 始终是其任务唯一仅有的成员。 |
-------------------------------------------------------------------------------------------------------------------------------------
======================================================
根据前面几篇关于启动模式的篇章做以下总结:
不同点(1)singleTask栈内可以有其他Activity,singleInstance栈内只能有它自己
(2)singleTask受taskAffinity影响,singleInstance不受taskAffinity影响
(3)singleTask如果实例已经存在,则会销毁在其之上的其它Activity,singleInstance则只会简单的把该栈移到最顶端,不会销毁Activity
相同点 :全局单例