如何恢复暂停的 Android 应用程序(应用程序而不是 Activity)

发布于 2024-11-09 18:34:25 字数 149 浏览 0 评论 0原文

如何让我的 Android 应用程序恢复到上次所在的位置,即视图中的 Activity 等?如果我显示一个活动并按主页按钮,然后再次启动我的应用程序,它将返回到启动活动。我希望它像 iPhone 应用程序一样工作,它暂停在适当的位置并恢复到用户上次所在的位置。

谢谢。

How can I have my Android app resume where it last was, i.e. the Activity in view, etc.? If I am showing an Activity and press the Home button and then launch my app again it returns to the startup activity. I'd like it to work like an iPhone app where it suspends in place and resumes back to where the user last was.

Thank you.

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

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

发布评论

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

评论(3

箹锭⒈辈孓 2024-11-16 18:34:25

您可能需要设置 android:alwaysRetainTaskState="true “对于清单中的根活动。从文档中:

任务的状态是否
该活动将永远是
由系统维护 — 如果为“true”
将会是,并且如果系统
允许将任务重置为其
某些情况下的初始状态。
默认值为“假”。这
属性仅对
任务的根活动;它被忽略了
对于所有其他活动。

通常情况下,
系统清除任务(删除所有
上面的堆栈中的活动
根活动)在某些情况下
当用户重新选择该任务时
从主屏幕。通常,这
如果用户没有访问过则完成
完成一定时间的任务,
例如 30 分钟。

然而,当这
属性为“true”时,用户将始终
返回任务的最后状态,
不管他们如何到达那里。这
很有用,例如,在
像网络浏览器这样的应用程序
有很多状态(例如
多个打开的选项卡)用户会
不喜欢失败。

You may need to set android:alwaysRetainTaskState="true" for your root activity in the manifest. From the documentation:

Whether or not the state of the task
that the activity is in will always be
maintained by the system — "true" if
it will be, and "false" if the system
is allowed to reset the task to its
initial state in certain situations.
The default value is "false". This
attribute is meaningful only for the
root activity of a task; it's ignored
for all other activities.

Normally,
the system clears a task (removes all
activities from the stack above the
root activity) in certain situations
when the user re-selects that task
from the home screen. Typically, this
is done if the user hasn't visited the
task for a certain amount of time,
such as 30 minutes.

However, when this
attribute is "true", users will always
return to the task in its last state,
regardless of how they get there. This
is useful, for example, in an
application like the web browser where
there is a lot of state (such as
multiple open tabs) that users would
not like to lose.

顾忌 2024-11-16 18:34:25

请务必在 onPause 方法中保存所需的任何信息,并在 onResume 中检查该信息。这将使应用程序重新加载所传达的任何信息。有些小部件保留状态,但其他小部件则不然。

此外,当一个 Activity 被终止时,它的 onSaveInstanceState 方法会被调用。因此,任何需要存储到持久内存的事情都应该在那里完成。

Be sure to save any information you want in your onPause method and check that information in onResume. This will let the app reload any information that was communicated. Some widgets keep state but others don't.

Additionally when an an activity is killed it's onSaveInstanceState method is called. So anything that needs to be stored to persistent memory should be done there.

您的好友蓝忘机已上羡 2024-11-16 18:34:25

虽然我正在使用 Mono for Android,但我也有类似的问题。

在网络上研究了一段时间后,您可以设置要使用单例模式创建的活动。这将导致您的应用程序仅创建一个实例,并且 - 如果您返回到该 Activity - 这将为您加载该 Activity 的最后状态。

属性配置 Activity 来完成此操作

您可以通过使用android:alwaysRetainTaskState="true"

 android:launchMode="singleTask" //(preffered, or "singleInstance")

Singletask 是最好的,因为 singleInstance 更适合基于单活动的应用程序。

就我而言,设置启动模式使这个婴儿摇滚如我所愿,但我不知道这是否是 Mono for Android 或 Android 问题的根源。.

注意:您将需要创建一个如果您想在某个时候创建​​活动的干净/新鲜/新实例,请使用初始化方法或类似的方法。

Although i'm working with Mono for Android i had a similar question.

After some time researching on the web you can setup an Activity to be created using the singleton pattern. This will cause your app to only create one instance and - if you go back to the activity - this will load the last state of that activity for you.

You can do this by configuring the activity with the

android:alwaysRetainTaskState="true"

and the

 android:launchMode="singleTask" //(preffered, or "singleInstance")

properties.

Singletask is the best since singleInstance is more suitable for single-activity-based apps.

In my case setting the launchmode made this baby rock as i wanted but i don't know if this is a Mono for Android or Android issue at the root..

Be aware: you will need to create an initialize method or something alike if you'd like to create a clean/fresh/new instance of the activity at some point.

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