Android:当应用程序被杀死时,如何设置新启动的入口点?
我使用仅包含静态字段的单独类来存储当前应用程序数据。 它的部分内容是在应用程序启动时从 sharedpreferences
填充的。其余的是诸如某些操作的结果之类的数据,用于进一步浏览这些结果(使用结果的多个活动)。
我可以转到主屏幕,启动其他应用程序等,当我返回到我自己的应用程序时,它就能正常工作。
但是,自从有了新的错误报告
功能后,我收到了一些与nullreference
错误相关的错误报告。 null
的对象是对上述单独类中静态字段的引用。
由于我无法重现该错误,我倾向于认为这是由于应用程序由于内存不足而被终止,并且当它重新启动时,它从用户当前所在的活动中调用 oncreate
。但是单独类中的所有静态数据都不会恢复,因此会崩溃。
我想知道:有没有办法强制应用程序完全“重新启动”,并且如果它被杀死,则不从上次使用的活动开始?或者这是标准行为? 我可以通过编程来做到这一点吗?就像当静态字段为空时,重新启动应用程序?
I am using a separate class with only static fields, to store current application data.
It is partly populated from sharedpreferences
on application startup. The rest is data like results of some action, used for further browsing these results (multiple activities that use the results).
I can go to the home screen, start other applications etc. and when I return to my own application it just works correctly.
However, since the new Error Reporting
feature I get some bug reports all related to a nullreference
error. The object that is null
is a reference to the static field in the mentioned separate class.
Since I cannot reproduce the bug I am inclined to think this is due to the application getting killed due to low memory, and when it relaunches it calls the oncreate
from the activity that the user was currently in. However all the static data in the separate class is not restored and thus it crashes.
I would like to know: Is there a way to force the application to "restart" completely, and not start with the last used activity if it gets killed? Or is that standard behaviour?
Can I do this programmatically? Like when the static fields are null, restart app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重新启动用户所在位置的 Activity 是正常行为 - 其想法是让用户看起来就像应用程序从未关闭过一样。您可以查看两件事:
该捆绑包与在
onCreate()
中传递给 Activity 的捆绑包相同。然后,您可以从中获取任何必要的信息并恢复静态类中的值。另一种方法是简单地检查任何活动的
onResume()
方法中的值。如果这些值是 null 或在某种程度上是错误的,那么您可以调用 start 原始活动并finish()
调用正在启动的活动。Restarting the activity where the user was is normal behaviour - the idea is to make it look to the user like the app was never closed. There are two things you can look at:
That bundle is the same one that gets passed to the activity in
onCreate()
. You can then get any necessary information out of it and restore the values in the static class.The other way is to simply check the values in the
onResume()
method of any of your activities. If the values are null or wrong in some way, then you can call start the original activity andfinish()
the one being started.