Android的singleTask或singleInstance启动模式? [英] Android singleTask or singleInstance launch mode?

查看:207
本文介绍了Android的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.

不过,我想这个搜索活动只有一次,比如在堆栈上,以便用户可以搜索并多次单击后退将其返回到previouse认为,他们是在他们开始寻找(而不是回去之前,以previouse搜索结果)

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)

无论是singleTask和singelInstance发射方式似乎做什么,我想这么林不知道哪一个,我应该使用为了这个目的,为什么?

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?

推荐答案

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

From the Application Fundamentals page of the Android dev guide:

在默认情况下,所有在活动   应用为每个具有亲和性   其他 - 也就是说,有一个preference   它们全部属于同一个   任务。

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.

一个singleInstance活动看台   单独作为唯一活动的   任务。如果它开始另一个活动,   该活动将启动成   不同任务,不管其   发射方式 - 仿佛   FLAG_ACTIVITY_NEW_TASK是在   意图。在所有其他方面,该   singleInstance模式是相同的   singleTask。

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

如上所述,从未有更   比一singleTask或一个实例   singleInstance活动,使   例如预计将处理所有的新   意图。 AsingleInstance活动   总是在堆栈的顶部   (因为它是在唯一的活动   任务),所以它总是在位置上   处理这个意图。然而,一个   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.)

由于是两种发射方式的活动从未多个实例,返回按钮会一直带你到活动的现有实例你的情况。

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.

这是很重要的区别是,singleTask并不需要创建一个新的任务时,选择一些正在启动的新活动。也不会对每次删除新任务上的后退按钮。

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.

由于您的活动栈并全部属于一个用户的任务,并且它不喜欢你的声音有一个复杂的意向结构,其中singleInstance可能是有益的总是处理它们,我会建议使用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.

下面是一个很好的博客文章获取更多信息,并记为图像:<一href="http://blog.akquinet.de/2010/02/17/android-activites-and-tasks-series-an-introduction-to-androids-ui-component-model/">Android活动和任务系列 - 介绍Android的UI组件模型

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的singleTask或singleInstance启动模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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