gpt4 book ai didi

Android singleTask 还是 singleInstance 启动模式?

转载 作者:IT老高 更新时间:2023-10-28 13:08:42 29 4
gpt4 key购买 nike

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

但是我希望此搜索 Activity 在堆栈中只有一个实例,以便用户可以多次搜索并单击返回将他们返回到他们在开始搜索之前所在的上一个 View (而不是返回到上一个搜索结果)

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

最佳答案

来自 Application Fundamentals Android 开发指南页面:

By default, all the activities in anapplication have an affinity for eachother — that is, there's a preferencefor them all to belong to the sametask.

A "singleInstance" activity standsalone as the only activity in itstask. If it starts another activity,that activity will be launched into adifferent task regardless of itslaunch mode — as ifFLAG_ACTIVITY_NEW_TASK was in theintent. In all other respects, the"singleInstance" mode is identical to"singleTask".

As noted above, there's never morethan one instance of a "singleTask" or"singleInstance" activity, so thatinstance is expected to handle all newintents. A "singleInstance" activityis always at the top of the stack(since it is the only activity in thetask), so it is always in position tohandle the intent. However, a"singleTask" activity may or may nothave other activities above it in thestack. If it does, it is not inposition to handle the intent, and theintent is dropped. (Even though theintent is dropped, its arrival wouldhave caused the task to come to theforeground, where it would remain.)

4 Activities in a Task

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

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

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

这是一篇很好的博文,可获取更多信息,以及图片的功劳:Android Activities and Tasks series – An introduction to Android’s UI component model

关于Android singleTask 还是 singleInstance 启动模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3219726/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com