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

查看:203
本文介绍了Android单机或单机启动模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

解决方案

p>从Android开发人员指南的


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?

解决方案

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

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

这篇关于Android单机或单机启动模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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