简体   繁体   English

android中的singleTask launchMode无法正常工作

[英]singleTask launchMode in android not working

So, I have 4 activities Activity Activity1 , Activity2 , Activity3 and Activity4 . 所以,我有4个活动Activity Activity1Activity2Activity3Activity4 I start from Activity1 then on some event I start Activity2 then on some event on Activity2 I start Activity3 as a new task as 从我做起Activity1然后在一些事件中,我开始Activity2 ,然后对一些事件Activity2我开始Activity3作为一个新的任务,

public void onClick(View v) {
    Intent intent = new Intent(Activity2.this, Activity3.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

Here Activity2 launchMode is decalared as singleTask. 这里Activity2 launchMode被解析为singleTask。

Then I navigate from Activity3 to Activity4 然后我从Activity3导航到Activity4

When I start Activity2 from Activity4 I believe it should have backstack like 当我从Activity4启动Activity2 ,我相信它应该有像Backstack一样

Task A 任务A.

|Activity2| |活性2 |

|Activity1| |活动1 |

Task B 任务B.

|Activity4| | Activity4 |

|Activity3| | Activity3 |

as shown in image below 如下图所示 在此输入图像描述

but instead new instance of Activity2 is added to the current task as 而是将新的Activity2实例添加到当前任务中

Task B 任务B.

|Activity2| |活性2 |

|Activity4| | Activity4 |

|Activity3| | Activity3 |

Task A 任务A.

|Activity2| |活性2 |

|Activity1| |活动1 |

can someone please help me understand this? 请有人帮我理解这个吗?

Android will not create a new task unless the taskAffinity of the Activity you are trying to launch is different from the taskAffinity of the current task. Android将不会创建一个新的任务,除非taskAffinity中的Activity你都争相推出从不同taskAffinity当前任务的。 taskAffinity takes precendence over anything else. taskAffinity优先于其他任何东西。

If you really want to start a separate task, you'll need to adjust the taskAffinity of the Activity3 like this: 如果你真的想要开始一个单独的任务,你需要调整Activity3taskAffinity ,如下所示:

<activity android:name=".Activity3"
          android:taskAffinity=""/>

Also, even though Activity4 is declared with launchMode="singleTask" , Android will not create a new task for it unless if has a different taskAffinity from the other activities. 此外,即使使用launchMode="singleTask"声明Activity4 ,Android也不会为它创建新任务,除非它与其他活动具有不同的taskAffinity

Please be aware that creating multiple tasks for the same application has a lot of side-effects. 请注意,为同一应用程序创建多个任务会产生很多副作用。 You should provide a uniqyue icon and a unique label for the root Activity in each task, otherwise the user will be confused when he wants to return to your app (he won't be able to determine which of the various tasks is the correct one). 您应该为每个任务中的根Activity提供uniqyue图标和唯一标签,否则当用户想要返回到您的应用时会感到困惑(他将无法确定哪个是正确的任务) )。 Also, taskReparenting can occur which will unexpectedly move activities from one task to another. 此外, taskReparenting可能会发生,意外地将活动从一个任务移动到另一个任务。 This is all very complex stuff and most developers don't completely understand it and therefore get it wrong. 这是非常复杂的东西,大多数开发人员并不完全理解它,因此弄错了。

Also, the use of FLAG_ACTIVITY_MULTIPLE_TASK is certainly wrong. 此外,使用FLAG_ACTIVITY_MULTIPLE_TASK肯定是错误的。 The only type of application that needs to use this is a launcher. 需要使用它的唯一类型的应用程序是启动器。 If you launch multiple tasks with the same Activity in it, you will never be able to return to a specific one programatically, and will totally confuse your user. 如果您使用相同的Activity启动多个任务,您将永远无法以编程方式返回到特定的任务,并且会完全混淆您的用户。 This is definitely not what you want. 这绝对不是你想要的。

In most cases you don't need multiple tasks, it usually causes more problems than it solves. 在大多数情况下,您不需要多个任务,它通常会导致比解决的问题更多的问题。 Please explain why you think you need multiple tasks. 请解释为什么您认为自己需要多项任务。

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

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