简体   繁体   English

从单一实例活动启动活动

[英]Launching activity from a singleinstance activity

I have 3 activities:我有 3 个活动:

A, B, C甲、乙、C

A has singleInstance launchMode. A 具有singleInstance launchMode。

A is the main activity, which launches B, B launches C, then I press the home button and open the app again. A 是主要活动,它启动 B,B 启动 C,然后我按下主页按钮并再次打开应用程序。

A is opened, but when A tries to launch B, the existing stack is opened instead (returns to existing C) A 被打开,但是当 A 尝试启动 B 时,现有的堆栈被打开(返回到现有的 C)

Is this the expected behaviour?这是预期的行为吗?

If A is singleInstance , when A launches B, Android automatically adds FLAG_ACTIVITY_NEW_INSTANCE to the Intent (because A is singleInstance ) and because of that it creates a new task with B at the root of the task.如果 A 是singleInstance ,当 A 启动 B 时,Android 会自动将FLAG_ACTIVITY_NEW_INSTANCE添加到Intent (因为 A 是singleInstance ),因此它会创建一个新任务,B 位于任务的根目录。 Then B launches C. Andriod launches C into the same task as B (this is the normal behaviour).然后 B 启动 C。Andriod 将 C 启动到与 B 相同的任务中(这是正常行为)。 So now you have 2 tasks: One with just A in it, and another which has B at the root, with C on top of that.所以现在你有 2 个任务:一个只有 A,另一个在根部有 B,最上面有 C。

Now you go back to the task containing A. When A launches B again, Android again adds FLAG_ACTIVITY_NEW_TASK to the Intent ).现在你 go 回到包含 A 的任务。当 A 再次启动 B 时,Android 再次将FLAG_ACTIVITY_NEW_TASK添加到Intent )。 Then, because FLAG_ACTIVITY_NEW_TASK is set, Android looks for an existing task that has B at the root.然后,因为设置了FLAG_ACTIVITY_NEW_TASK ,所以 Android 会查找根部有 B 的现有任务。 Since it finds one, it simple brings that task to the foreground in whatever state the task was in when that task went to the background.因为它找到了一个,所以它很简单地将那个任务带到前台,无论 state 当那个任务进入后台时任务所在的位置。 This is the normal and expected behaviour.这是正常的和预期的行为。

NOTE: This is just one more reason NOT to use the special launch modes singleInstance and singleTask , because they have lots of nasty, undocumented, misunderstood side-effects.注意:这只是不使用特殊启动模式singleInstancesingleTask的又一个原因,因为它们有很多讨厌的、未记录的、被误解的副作用。

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

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