简体   繁体   English

android - 启动模式为“singleInstance”的两个活动

[英]android - Two activities with launch modes as 'singleInstance'

Suppose there are two activities in a single android application with launch modes as 'singleInstance'.假设在单个 android 应用程序中有两个活动,启动模式为“singleInstance”。 Assume an example below.假设下面的一个例子。

I am navigating from activity A -> B( launchMode="singleInstance" ).我从活动 A -> B( launchMode="singleInstance" ) 导航。 Now from activity B -> C. Finally, I navigate from activity C -> D( launchMode="singleInstance" ).现在从活动 B -> C。最后,我从活动 C -> D( launchMode="singleInstance" ) 导航。

Now we know instance of activity B will be created in a different task, and A & C will be in the different task.现在我们知道活动 B 的实例将在不同的任务中创建,而 A 和 C 将在不同的任务中。

Now, my question is, in which task instance of activity D would be placed.现在,我的问题是,将放置活动 D 的哪个任务实例。 Will it be with activity B, or some other task would be created for activity D.是与活动 B 一起,还是会为活动 D 创建一些其他任务。

Thanks.谢谢。

I'll bold the answer to your question if you don't want to read the clarification.如果您不想阅读说明,我会将您问题的答案加粗

When using launchMode="singleInstance" , there's two things to keep in mind:使用launchMode="singleInstance" ,需要记住两件事:

  • The Activity will always be created in a new task活动将始终在新任务中创建
  • All Activities launched from this Activity will be created in a separate task从此活动启动的所有活动都将在单独的任务中创建

As such, an Activity with launchMode of singleInstance will always be isolated in it's own task.因此,launchMode 为singleInstance的 Activity 将始终在它自己的任务中被隔离。 There won't be another Activity inside of that task.该任务中不会有另一个活动。

So with your example from your question of Activities A, B, C, and D:因此,以您的活动 A、B、C 和 D 问题中的示例为例:

  • Activity A launches Activity B活动 A 启动活动 B
  • Activity B is launchMode="singleInstance" so it's on a new task活动 B 是launchMode="singleInstance"所以它是一个新任务
  • Activity B launches Activity C活动 B 启动活动 C
  • Activity C is launched in the same task as Activity A活动 C 在与活动 A 相同的任务中启动
  • Activity C launches Activity D活动 C 启动活动 D
  • Activity D is launchMode="singleInstance" so it's on a new task活动 D 是launchMode="singleInstance"所以它在一个新任务上

From what happened here, you have one a task that stores the launchMode="standard" Activity A and Activity C. Activity B is in it's own task.从这里发生的事情来看,您有一个任务存储launchMode="standard"活动 A 和活动 C。活动 B 在它自己的任务中。 Activity D is in it's own task.活动 D 在它自己的任务中。

Therefore, if you choose to Back out of these Activities, you'll notice that:因此,如果您选择退出这些活动,您会注意到:

  • Activity D is backed and Activity C appears活动 D 得到支持,活动 C 出现
  • Activity C is backed and Activity A appears活动 C 得到支持,活动 A 出现

This happens because Activity C is on the same task as Activity A.发生这种情况是因为活动 C 与活动 A 执行相同的任务。

Also, Activity D definitely won't be in the same task as Activity B because Activity B's task is meant only for Activity B due to launchMode="singleInstance" .此外,活动 D 绝对不会与活动 B 处于同一任务中,因为活动 B 的任务仅适用于活动 B,因为launchMode="singleInstance"

Keep in mind that there can be any number of tasks being held in the background at once.请记住,后台可以同时执行任意数量的任务。 Just that if there's too many being held or if the system requires memory, it'll start destroying these background Activities across your multiple tasks.只是如果有太多被持有或者如果系统需要内存,它会开始在您的多个任务中破坏这些后台活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM