Android 第5种启动模式:singleInstancePerTask
随着 Android 版本的更新,应用启动模式逐渐丰富。在 Android 12 中,新增了一种启动模式——singleInstancePerTask
。它是继 standard
、singleTop
、singleTask
和 singleInstance
之后的第五种启动模式。本文将详细介绍 singleInstancePerTask
的工作原理、使用场景以及与其他启动模式的区别。
一、什么是 singleInstancePerTask?
singleInstancePerTask
允许一个 Activity 作为任务栈的根 Activity 来运行,并且它只会在这个任务栈中有一个实例。不过,与 singleTask
不同的是,singleInstancePerTask
可以在不同的任务栈中创建多个实例。如果启动该 Activity 时设置了 FLAG_ACTIVITY_MULTIPLE_TASK
或 FLAG_ACTIVITY_NEW_DOCUMENT
标志,那么每次启动时都会创建新的任务栈。
在 Android SDK 中,singleInstancePerTask
的定义如下:
<!-- Activity can only be running as the root activity of the task, and therefore
there will only be one instance of this activity in a task. However, unlike
singleTask, this activity can be started in multiple instances in different tasks. -->
<enum name="singleInstancePerTask"