Android:当应用程序被杀死时,如何设置新启动的入口点?

发布于 2024-09-03 16:02:41 字数 518 浏览 3 评论 0原文

我使用仅包含静态字段的单独类来存储当前应用程序数据。 它的部分内容是在应用程序启动时从 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 技术交流群。

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

发布评论

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

评论(1

无名指的心愿 2024-09-10 16:02:41

重新启动用户所在位置的 Activity 是正常行为 - 其想法是让用户看起来就像应用程序从未关闭过一样。您可以查看两件事:

protected void onSaveInstanceState(Bundle outState){
    // This gets called by the system when it's about to kill your app
    // Put all your data in the outState bundle
}

该捆绑包与在 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:

protected void onSaveInstanceState(Bundle outState){
    // This gets called by the system when it's about to kill your app
    // Put all your data in the outState bundle
}

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 and finish() the one being started.

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