Android 会将被终止的 Activity 的状态保存多长时间?

发布于 2024-11-30 06:16:03 字数 323 浏览 1 评论 0原文

我刚刚做了一个小测试:我启动了我的应用程序,转到某个屏幕,按下主页按钮并通过高级任务杀手终止了该进程。

现在,如果我稍后返回我的应用程序,我就会回到那个屏幕。我还知道在这种情况下会调用 onRestoreInstanceState() ,因为我已经使用过这个方法很多次了。

然而,出于好奇,我还是做了同样的事情,但让我的手机闲置了一段时间(一个小时左右)。当我重新启动应用程序时,它直接进入主要活动。

所以我的问题是:Android 会将保存的状态保留多长时间?或者是什么决定了它是否扔掉它?我已经发现它与锁屏开/关无关。

亲切的问候, 海蜇

I just did a little test: I started my app, went to a certain screen, pressed the home button and killed the process via Advanced Task Killer.

Now, if I go back to my app just a few moments later, I come back to that very screen. I also know that onRestoreInstanceState() is called in this case, as I have played around with this method quite a bit.

However, just out of curiosity, I did all the same, but let my phone lie around for some time (an hour or something). When I restarted my app, it went straight to the main activity.

So my question is: for how long does Android keep the saved state? or what determines if it throws it away or not? I already figured out it had nothing to do with lockscreen on/off.

Kind regards,
jellyfish

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

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

发布评论

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

评论(3

风为裳 2024-12-07 06:16:03

活动恢复是因为高级任务杀手等应用程序使用“非法”方法来终止应用程序。当内存不足时,Android 会以类似的方式杀死应用程序,并且当再次启动被杀死的应用程序时,其状态会恢复。

Android 将应用程序状态保留一段时间。通常大约是 15-30 分钟,但我认为这取决于设备。但是您可以使用 android:alwaysRetainTaskState="true" 属性。

The activity is restored because such applications as Advanced Task Killer use "illegal" methods for killing applications. Android kills application is a similar way when it's low on memory and when killed application is launched again its state is restored.

Android keeps an application state for some time. Usually it's about 15-30 minutes but I think it depends on the device. But you can ask Android to keep an activity state "forever" (until the activity is finished) using android:alwaysRetainTaskState="true" attribute.

无可置疑 2024-12-07 06:16:03

例如,当您使用 Advanced Task Killer 终止应用程序时,该应用程序下次会以主活动重新启动。我认为应用程序将状态保存在文件中以恢复相同的活动,即使它被杀死!

When you killed an application with Advanced Task Killer for example, the application restart with the main activity the next time. I suppose that the application save the state in file to restore the same activity even if it killed!

金兰素衣 2024-12-07 06:16:03

当 Android 需要该 Activity 所持有的资源(通常是内存)并且该 Activity 不在前台时,它会销毁该 Activity。涉及更多标准,但这是一般情况。除了“当它决定需要时”之外,没有预定义的时间限制或标准。该应用程序可能几乎立即终止,也可能无限期地保持运行。您永远不应该假设您的应用程序将永远被杀死,并且您永远不应该假设您的应用程序永远不会被杀死。

Android will dispose of the activity when it needs the resources that the activity is holding (usually memory) and the activity is not in the foreground. There's more criteria involved, but that's the general case. There's no predefined time limit or criteria other than "when it decides it needs to". The app could die almost immediately or it could stay up indefinitely. You should never assume your app will ever be killed and you should never assume your app will never be killed.

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