Android中的singleTask launchMode无法正常工作 [英] singleTask launchMode in android not working

查看:64
本文介绍了Android中的singleTask launchMode无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有4个活动,活动 Activity1 Activity2 Activity3 Activity4 .我从 Activity1 开始,然后在某个事件中启动 Activity2 ,然后在 Activity2 上的某个事件中,我启动 Activity3 作为

So, I have 4 activities Activity Activity1, Activity2, Activity3 and Activity4. 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

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);
}

此处 Activity2 launchMode被声明为singleTask.

Here Activity2 launchMode is decalared as singleTask.

然后我从 Activity3 导航到 Activity4

当我从 Activity4 启动 Activity2 时,我认为它应该像

When I start Activity2 from Activity4 I believe it should have backstack like

任务A

|活动2 |

|活动1 |

任务B

| Activity4 |

|Activity4|

| Activity3 |

|Activity3|

如下图所示

,而是将 Activity2 的新实例作为

任务B

|活动2 |

| Activity4 |

|Activity4|

| Activity3 |

|Activity3|

任务A

|活动2 |

|活动1 |

有人可以帮助我理解这一点吗?

can someone please help me understand this?

推荐答案

除非您尝试启动的 Activity taskAffinity ,否则Android不会创建新任务.与当前任务的 taskAffinity 不同. taskAffinity 优先于其他任何事情.

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. taskAffinity takes precendence over anything else.

如果您确实要启动单独的任务,则需要像这样调整 Activity3 taskAffinity :

If you really want to start a separate task, you'll need to adjust the taskAffinity of the Activity3 like this:

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

此外,即使使用 launchMode ="singleTask" 声明了 Activity4 ,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.

请注意,为同一应用程序创建多个任务会产生很多副作用.您应该在每个任务中为根 Activity 提供一个uniqyue图标和一个唯一标签,以表示根 Activity ,否则用户想要返回到您的应用程序时会感到困惑(他将无法确定哪个各项任务才是正确的).另外,可能会发生 taskReparenting ,这会将活动意外地从一个任务转移到另一个任务.这些都是非常复杂的东西,大多数开发人员并不完全理解它,因此弄错了.

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). Also, taskReparenting can occur which will unexpectedly move activities from one task to another. This is all very complex stuff and most developers don't completely understand it and therefore get it wrong.

此外,使用 FLAG_ACTIVITY_MULTIPLE_TASK 肯定是错误的.唯一需要使用此应用程序的类型是启动器.如果您在其中启动具有相同 Activity 的多个任务,则将永远无法以编程方式返回到特定任务,这将使您的用户完全困惑.这绝对不是您想要的.

Also, the use of FLAG_ACTIVITY_MULTIPLE_TASK is certainly wrong. 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. 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.

这篇关于Android中的singleTask launchMode无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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