Android singleTask 还是 singleInstance 启动模式?

发布于 2024-09-08 18:40:55 字数 234 浏览 2 评论 0原文

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

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

singleTask 和 singleInstance 启动模式似乎都能满足我的要求,所以我不确定应该使用哪一种来实现此目的,为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

邮友 2024-09-15 18:40:55

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

默认情况下,一个中的所有活动
每个应用程序都有亲和力
其他——也就是说,有一个偏好
让他们都属于同一个
任务。

“singleInstance”活动代表
单独作为其唯一的活动
任务。如果它开始另一个活动,
该活动将在
不同的任务,无论其
启动模式——就像
FLAG_ACTIVITY_NEW_TASK 位于
意图。在所有其他方面,
“singleInstance”模式等同于
“单一任务”。

如上所述,没有更多
比“singleTask”的一个实例或
“singleInstance”活动,以便
实例预计能够处理所有新的
意图。 “singleInstance”活动
总是在栈顶
(因为这是唯一的活动
任务),所以它总是处于位置
处理意图。然而,一个
“singleTask”活动可能会也可能不会
在其之上还有其他活动
堆。如果是的话,它不在
处理意图的位置,以及
意图被放弃。 (尽管
意图被放弃,它的到来将
导致任务来到
前景,它将保留在哪里。)

任务中的 4 个活动

因为具有任一活动的活动实例绝不会超过一个启动模式下,后退按钮将始终将您带到您案例中 Activity 的现有实例。

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

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

这是一篇很好的博客文章,提供更多信息,以及图片的来源:Android 活动和任务系列 – Android 的 UI 组件模型简介

From the Application Fundamentals page of the Android dev guide:

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.)

4 Activities in a Task

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.

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.

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.

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

夜清冷一曲。 2024-09-15 18:40:55

以一种简单的方式 -

singleTask

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

注意:虽然活动在新任务中启动,但“后退”按钮
仍然使用户返回到之前的活动。

singleInstance-

“singleTask” 相同,但系统不会在持有实例的任务中启动任何其他活动。该活动始终是其任务中唯一的成员; 此人启动的任何活动都会在单独的任务中打开

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 instance of the activity can exist at a time.

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.

影子是时光的心 2024-09-15 18:40:55

singleTasksingleInstance 活动只能开始一个任务。它们始终位于活动堆栈的根部。此外,设备一次只能容纳一个 Activity 实例 - 只能执行一项这样的任务。
了解更多信息 android:launchMode

singleTask and singleInstance activities 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文