Android singleTask 还是 singleInstance 启动模式?
我有一个应用程序,其主要活动是列表,然后您可以单击项目来打开该项目的详细视图。我还有一个与主要活动类似并且按预期工作的搜索活动。
但是,我希望此搜索活动在堆栈上只有一个实例,以便用户可以多次搜索,然后单击“后退”会将他们返回到开始搜索之前所在的上一个视图(而不是返回到上一个搜索结果)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Android 开发指南的应用程序基础页面:
因为具有任一活动的活动实例绝不会超过一个启动模式下,后退按钮将始终将您带到您案例中 Activity 的现有实例。
一个重要的区别是“singleTask”不需要为选择某些内容时启动的新活动创建新任务。也不必每次都删除后退按钮上的新任务。
由于您的 Activity 堆栈确实全部属于一个用户“任务”,并且听起来您没有一个复杂的 Intent 结构,其中 singleInstance 可能有利于始终处理它们,因此我建议使用 singleTask 启动模式。
这是一篇很好的博客文章,提供更多信息,以及图片的来源:Android 活动和任务系列 – Android 的 UI 组件模型简介
From the Application Fundamentals page of the Android dev guide:
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
以一种简单的方式 -
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. Onlyone instance
of the activity can exist at a time.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
和singleInstance
活动只能开始一个任务。它们始终位于活动堆栈的根部。此外,设备一次只能容纳一个 Activity 实例 - 只能执行一项这样的任务。了解更多信息 android:launchMode。
singleTask
andsingleInstance
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.