为什么行为不同?-android:launchMode ="singleTask",android:taskAffinity =“"和意图.FLAG_ACTIVITY_NEW_TASK [英] Why Behaviours are different ?- android:launchMode="singleTask" , android:taskAffinity="" And Intent.FLAG_ACTIVITY_NEW_TASK

查看:82
本文介绍了为什么行为不同?-android:launchMode ="singleTask",android:taskAffinity =“"和意图.FLAG_ACTIVITY_NEW_TASK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个活动-A,B,C,D

I have Four Activity - A,B,C,D

我以-> A-B-C-D-B的方式称呼这四个活动.(指定方式)

I am calling these four activity in manner --> A-B-C-D-B. (Specified Manner)

我有三种情况.

第一:-我仅在B活动中定义 android:launchMode ="singleTask" .我正在以上述指定的方式通过 Intent 调用所有活动.

1st :- I am defining android:launchMode="singleTask" only in B Activity. And I am calling all activity via Intent in above specified manner.

现在,首先调用A-B-C-D, BackStack任务1:A-B-C-D,

Now First calling A-B-C-D , BackStack Task 1 : A-B-C-D,

现在,我再次调用B,然后调用 BackStack Task 1:A-B .C和D活动在这里被摧毁.

Now I again call B, Then BackStack Task 1 : A-B. Here C and D Activities are destroyed.

2nd:-我正在定义 android:launchMode ="singleTask" &B活动中的 android:taskAffinity =" .我正在以上述指定的方式通过 Intent 调用所有活动.

2nd :- I am defining android:launchMode="singleTask" & android:taskAffinity="" in B Activity. And I am calling all activity via Intent in above specified manner.

现在首先调用A-B-C-D, BackStack任务1:A

Now First calling A-B-C-D , BackStack Task 1 : A

                                  Task 2 : B-C-D

现在我再次调用B,然后 BackStack Task 1:A

Now I again call B, Then BackStack Task 1 : A

                                Task 2 : B ,Here C and D Activities are destroyed.

3rd:-我正在定义 Intent.FLAG_ACTIVITY_NEW_TASK &B活动中的 android:taskAffinity =" .我正在以上述指定的方式通过 Intent 调用所有活动.

3rd :- I am defining Intent.FLAG_ACTIVITY_NEW_TASK & android:taskAffinity="" in B Activity. And I am calling all activity via Intent in above specified manner.

现在首先调用A-B-C-D, BackStack任务1:A

Now First calling A-B-C-D , BackStack Task 1 : A

                                  Task 2 : B-C-D

现在我再次调用B,然后 BackStack Task 1:A

Now I again call B, Then BackStack Task 1 : A

                                Task 2 : B-C-D , Here **Can't call B again**

在这里它说FLAG_ACTIVITY_NEW_TASK产生与"singleTask"相同的行为- https://developer.android.com/guide/components/activities/tasks-and-back-stack.html

And here It says FLAG_ACTIVITY_NEW_TASK produces the same behavior as the "singleTask" - https://developer.android.com/guide/components/activities/tasks-and-back-stack.html

那么哪些是正确的方案?我做对了,或者我误会了一些东西.

So Which are correct scenarios? I am getting it right or I am misunderstanding something.

推荐答案

我已经复制了您所描述的行为.

I've reproduced the behaviour as you've described.

第一种情况基本上没有记载,因为 singleTask Activity 应该始终是任务的根源.由于Android在确定如何启动和在何处启动 Activity 时使用了 taskAffinity ,因此您的 singleTask Activity 正在启动到现有任务,但不作为根 Activity .这使行为不同于已记录的行为.当您从 D 启动 B 时,Android需要在调用的过程中将 Intent 传递给 B onNewIntent(),它不能用于位于 B 顶部的 C D .因此,Android完成 C D ,然后在 B 上调用 onNewIntent().

The first scenario is basically undocumented because a singleTask Activity should always be the root of a task. Because Android uses taskAffinity when determining how and where to launch an Activity, your singleTask Activity is being launched into the existing task, but not as the root Activity. This makes the behaviour different from the behaviour that is documented. When you launch B from D, Android needs to deliver the Intent to B in a call to onNewIntent(), which it cannot do with C and D sitting on top of B. So Android finishes C and D and then calls onNewIntent() on B.

第二种情况显示您现在有2个任务,因为 singleTask Activity 作为根 Activity 启动到一个新任务中.同样,当 D 启动 B 时,Android需要通过调用 onNewIntent(),如果 C D 妨碍了它,则无法执行此操作.因此,Android完成 C D ,然后通过 onNewIntent()将 Intent 传递给 B 代码>.

The second scenario shows that you now have 2 tasks, because the singleTask Activity is launched into a new task as the root Activity. Again, when D launches B, Android needs to deliver the Intent to B by calling onNewIntent(), which it cannot do if C and D are in the way. So Android finishes C and D and then delivers the Intent to B in onNewIntent().

第三种情况显示了 FLAG_ACTIVITY_NEW_TASK 的用法.当 A 使用 FLAG_ACTIVITY_NEW_TASK 启动 B 时,Android将查找以 B 为根的现有任务.活动.由于找不到一个,因此它将在新任务中以根 Activity 的身份启动 B .当 D 使用 FLAG_ACTIVITY_NEW_TASK 启动 B 时,Android将查找以 B 作为根 Activity的任务,如果找到一个,它将把该任务带到前台,但不将 Intent 传递给 onNewIntent() .这只是将现有任务转到后台时所处的状态的一种方法.这就是您所看到的.此行为也已记录(分类),但是由于所有这些情况之间的相互依赖性非常复杂,因此未明确记录所有可能的情况.有时,您需要通过经验观察来发现其工作原理,而不是阅读文档.

The third scenario shows the use of FLAG_ACTIVITY_NEW_TASK. When A launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for an existing task that has B as the root Activity. Since it doesn't find one, it launches B in a new task as the root Activity. When D launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for a task that has B as the root Activity, and if it finds one, it brings that task to the foreground but does not deliver the Intent to onNewIntent(). This is just a way to bring an existing task to the foreground in the state that it was in when it went to the background. This is what you are seeing. This behaviour is also documented (sort-of), but due to the very complex interdependencies between all of these scenarios, all possible cases are not explicitly documented. Sometimes you need to discover how this works by empirical observation instead of reading the documentation.

感谢挑战.

这篇关于为什么行为不同?-android:launchMode ="singleTask",android:taskAffinity =“"和意图.FLAG_ACTIVITY_NEW_TASK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆