Android 设置 launchMode="singleTask" 与设置活动 launchMode="singleTop"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25773928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:43:04  来源:igfitidea点击:

Setting launchMode="singleTask" vs setting activity launchMode="singleTop"

androidandroid-activityandroid-manifestandroid-navigation

提问by NSouth

I have an app that is very hierarchical (activities are similar to League > Team > Position > Player) and so I've made each activity singleTopin order to keep navigation sensible and to prevent duplicate instances.

我有一个非常分层的应用程序(活动类似于联赛 > 球队 > 位置 > 球员),因此我制作了每个活动singleTop以保持导航合理并防止重复实例。

Now I'm making my second app and I've seen it suggested to declare my application to be singleTaskto prevent duplicate instances. Could someone help explain the advantages of each approach?

现在我正在制作我的第二个应用程序,我看到它建议将我的应用程序声明singleTask为防止重复实例。有人可以帮助解释每种方法的优点吗?

My new app is just an activity with 3 fragments and then I'll probably add a settings activity and maybe a FAQ.

我的新应用程序只是一个包含 3 个片段的活动,然后我可能会添加一个设置活动和一个常见问题解答。

EDIT: I just realized that singleTaskis NOT preventing duplicate instances of my app, as I had thought. Now looking for the right way to handle this...

编辑:我刚刚意识到这singleTask不会阻止我的应用程序的重复实例,正如我所想的那样。现在正在寻找处理这个问题的正确方法......

回答by CChi

I think your definition of singleTop and singleTask is a little off. SingleTop could produce a duplicate instance. Lets use your example, League > Team > Position > Player. If there is a button in the player screen that will take you to the league screen, it will become League > Team > Position > Player > League.

我认为您对 singleTop 和 singleTask 的定义有点偏离。SingleTop 可能会产生重复的实例。让我们以您的示例为例,联赛 > 球队 > 位置 > 球员。如果玩家屏幕中有一个按钮可以将您带到联赛屏幕,它将变为联赛 > 球队 > 位置 > 球员 > 联赛。

Whereas singleTask guarantees that only one instance of the activity can exist.

而 singleTask 保证只能存在一个活动实例。

回答by Selva

Android activity launchMode

Android 活动启动模式

4 modes...

4种模式...

"standard" "singleTop" "singleTask" "singleInstance"

“标准”“singleTop”“singleTask”“singleInstance”

The default mode is "standard".

默认模式是“标准”。

The modes fall into two groups. standard and singleTop comes in one side and singleTask and singleInstance comes in another side.

这些模式分为两组。standard 和 singleTop 位于一侧,而 singleTask 和 singleInstance 位于另一侧。

The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created. In case of singleTop too, a new instance is created but an instance of the activity is already in top of the stack, it wont create a new instance.

标准和单顶之间的主要区别在于标准,每次标准活动的新意图时,都会创建一个新实例。在 singleTop 的情况下,也会创建一个新实例,但 Activity 的实例已经在堆栈顶部,它不会创建新实例。

Actually, the issue comes , when we download an application from a server and launch it and open it from there itself. After launching the application, press home button. Then click the all programs and select the icon of the application from home screen. Then another activity will be created in the case of standard, but in singleTop , no new instance will be created.

实际上,当我们从服务器下载应用程序并启动它并从那里打开它时,问题就来了。启动应用程序后,按主页按钮。然后单击所有程序并从主屏幕选择应用程序的图标。然后在标准的情况下将创建另一个活动,但在 singleTop 中,将不会创建新实例。

The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's at the root of the activity stack, but other activities (necessarily "standard" and "singleTop" activities) can be launched into the same task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

“singleTask”和“singleInstance”模式也仅在一个方面彼此不同:“singleTask”活动允许其他活动成为其任务的一部分。它位于活动堆栈的根部,但其他活动(必须是“标准”和“单顶”活动)可以启动到同一任务中。另一方面,“singleInstance”活动不允许其他活动成为其任务的一部分。这是任务中唯一的活动。如果它启动另一个活动,该活动将被分配给不同的任务——就像 FLAG_ACTIVITY_NEW_TASK 在意图中一样。

http://smartandroidians.blogspot.in/2010/04/activity-launch-mode-in-android.html

http://smartandroidians.blogspot.in/2010/04/activity-launch-mode-in-android.html

回答by M.Hefny

I found the answer here: http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

我在这里找到了答案:http: //www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

"singleTop":

“单顶”:

The difference from 'standard' is, if an instance of activity already exists at the top of the current task and system routes intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object. Let's take the Twitter-oauth integration as example.

与“标准”的不同之处在于,如果当前任务的顶部已经存在一个活动实例并且系统将意图路由到此活动,则不会创建新实例,因为它将触发 onNewIntent() 方法而不是创建一个新对象。让我们以 Twitter-oauth 集成为例。

"singleTask":

“单一任务”:

A new task will always be created and a new instance will be pushed to the task as the root one. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. And if the user clicks the BACK key from the singleTask activity, the system will return the user to the previous activity.

将始终创建一个新任务,并将一个新实例作为根任务推送到该任务。但是,如果任何任务中存在任何活动实例,系统将通过 onNewIntent() 方法调用将意图路由到该活动实例。在这种模式下,活动实例可以被推送到同一个任务。如果用户从 singleTask 活动中单击 BACK 键,系统会将用户返回到上一个活动。

回答by since k saji

From Understanding Activity launch mode:

了解活动启动模式

standard (default) :- Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks. This is the common mode for most of the activities.

singleTop :- The difference from standard is, if an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.

singleTask:- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.

singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance's. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.

标准(默认):- 可以实例化活动类的多个实例,并且可以将多个实例添加到同一个任务或不同的任务中。这是大多数活动的常用模式。

singleTop :- 与标准的区别在于,如果当前任务的顶部已经存在活动的实例,并且系统将意图路由到此活动,则不会创建新实例,因为它将触发 onNewIntent() 方法而不是创建一个新对象。

singleTask:- 将始终创建一个新任务,并将一个新实例作为根推送到该任务。但是,如果任何任务中存在任何活动实例,系统将通过 onNewIntent() 方法调用将意图路由到该活动实例。在这种模式下,活动实例可以被推送到同一个任务。此模式对于充当入口点的活动很有用。

singleInstance:- 与 singleTask 相同,除了 no activity 实例可以推送到 singleInstance 的相同任务中。因此,具有启动模式的活动始终处于单个活动实例任务中。这是一种非常专业的模式,应该只用于完全作为一个活动实现的应用程序中。