Android singleTask 还是 singleInstance 启动模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3219726/
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:12:13  来源:igfitidea点击:

Android singleTask or singleInstance launch mode?

androidandroid-activitylaunchmultitasking

提问by Kman

I have an app that has a list as its main activity and then you can click items which opens a detailed view of that item. I also have a search activity that is similar to the main activity and works as intended.

我有一个应用程序,它有一个列表作为其主要活动,然后您可以单击项目以打开该项目的详细视图。我还有一个与主要活动类似的搜索活动,并按预期工作。

However I want this search activity to only have once instance on the stack so that users can search multiple times and clicking back would return them to the previouse view that they were on before they started searching (rather than go back to the previouse search results)

但是,我希望此搜索活动在堆栈中仅包含一次实例,以便用户可以多次搜索,然后单击返回会将他们返回到他们开始搜索之前所在的上一个视图(而不是返回到上一个搜索结果)

both the singleTask and singelInstance launch mode seems to do what I want so Im not sure which one I should be using for this purpose and why?

singleTask 和 singelInstance 启动模式似乎都在做我想要的,所以我不确定我应该为此目的使用哪一个,为什么?

回答by Guzba

From the Application Fundamentalspage of the Android dev guide:

从Android 开发指南的应用程序基础页面:

By default, all the activities in an application have an affinity for each other — that is, there's a preference for them all to belong to the same task.

A "singleInstance" activity stands alone as the only activity in its task. If it starts another activity, that activity will be launched into a different task regardless of its launch mode — as if FLAG_ACTIVITY_NEW_TASK was in the intent. In all other respects, the "singleInstance" mode is identical to "singleTask".

As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents. A "singleInstance" activity is always at the top of the stack (since it is the only activity in the task), so it is always in position to handle the intent. However, a "singleTask" activity may or may not have other activities above it in the stack. If it does, it is not in position to handle the intent, and the intent is dropped. (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.)

默认情况下,应用程序中的所有活动都具有相互关联性——也就是说,优先选择它们都属于同一个任务。

“singleInstance”活动作为其任务中的唯一活动独立存在。如果它启动了另一个 Activity,无论其启动模式如何,该 Activity 都将启动到不同的任务中——就好像 FLAG_ACTIVITY_NEW_TASK 在意图中一样。在所有其他方面,“singleInstance”模式与“singleTask”相同。

如上所述,“singleTask”或“singleInstance”活动的实例永远不会超过一个,因此预计该实例将处理所有新意图。“singleInstance”活动始终位于堆栈顶部(因为它是任务中唯一的活动),因此它始终处于处理意图的位置。但是,“singleTask”活动在堆栈中可能有也可能没有其他活动。如果是,则它无法处理该意图,并且该意图被丢弃。(即使意图被丢弃,它的到来也会导致任务出现在前台,它会留在那里。)

4 Activities in a Task

4 任务中的活动

Since there is never more than one instance of the Activity with either launch mode, the back button will always take you to the existing instance of the Activity in your case.

由于使用任一启动模式的 Activity 实例永远不会超过一个,因此在您的情况下,后退按钮将始终将您带到 Activity 的现有实例。

An important difference is that "singleTask" doesn't require the creation of a new task for the new Activities being launched when something is selected. Nor will it have to remove that new task on the back button each time.

一个重要的区别是“singleTask”不需要为选择某些内容时启动的新活动创建新任务。也不必每次都删除后退按钮上的新任务。

Since your Activity stack does all pertain to one user "task", and it doesn't sound like you have an intricate Intent structure where singleInstance may be beneficial to always handle them, I would suggest using the singleTask launch mode.

由于您的 Activity 堆栈确实都与一个用户“任务”有关,而且听起来您没有一个复杂的 Intent 结构,其中 singleInstance 可能有助于始终处理它们,因此我建议使用 singleTask 启动模式。

Here is a good blog post for more info, as well as credited for the image: Android Activities and Tasks series – An introduction to Android's UI component model

这是一篇很好的博客文章,可提供更多信息,并为图片归功于:Android 活动和任务系列 – Android 的 UI 组件模型介绍

回答by Tarun Varshney

In a simple way-

简单来说——

singleTask:

单一任务:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent()method, rather than creating a new instance. Only one instanceof the activity can exist at a time.

系统创建一个新任务并在新任务的根处实例化活动。但是,如果 Activity 的实例已存在于单独的任务中,系统会通过调用其onNewIntent()方法将意图路由到现有实例,而不是创建新实例。一次只能one instance存在一个活动。

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.

注意:虽然活动在新任务中开始,但返回按钮仍会将用户返回到上一个活动。

singleInstance-

单实例-

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

与 相同"singleTask",除了系统不会将任何其他活动启动到持有实例的任务中。活动始终是其任务的唯一成员;任何由此开始的活动都在单独的任务中打开

回答by Rupesh Yadav

singleTaskand singleInstanceactivities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
for more android:launchMode.

singleTasksingleInstance活动只能开始一项任务。它们始终位于活动堆栈的根部。此外,设备一次只能保存一个活动实例——只有一项这样的任务。
更多android:launchMode