如何恢复暂停的 Android 应用程序(应用程序而不是 Activity)
如何让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要设置 android:alwaysRetainTaskState="true “对于清单中的根活动。从文档中:
You may need to set android:alwaysRetainTaskState="true" for your root activity in the manifest. From the documentation:
请务必在 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.
虽然我正在使用 Mono for Android,但我也有类似的问题。
在网络上研究了一段时间后,您可以设置要使用单例模式创建的活动。这将导致您的应用程序仅创建一个实例,并且 - 如果您返回到该 Activity - 这将为您加载该 Activity 的最后状态。
属性配置 Activity 来完成此操作
您可以通过使用
android:alwaysRetainTaskState="true"
和
。
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
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.