请问Android应用程序上下文曾经被摧毁? [英] Does the Android Application Context ever get destroyed?

查看:197
本文介绍了请问Android应用程序上下文曾经被摧毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的Andr​​oid应用程序的应用程序上下文作为我的应用程序当前状态信息的存储区域。

我发现,在该领域,有这样的情况时,该信息就会消失在一些人的设备引起各种NullPointerException异常,因为我预计该数据将出现应用程序的简历,并开始重建必要的活动时。

这通常是当用户点击家的情况,做别的东西,然后最终飘荡回应用程序 - 它试图回到它原来所在之处,但应用程序上下文已经神秘地失去了所有的previously - 保存状态信息(在我的情况,一些整数和弦数)。

我知道这是一个非常模糊的问题,但是否有任何情况下,即使应用程序没有被终止应用程序上下文被完全破坏(比使用回到完全恢复了应用程序的用户除外)<? / p>

有没有更好的方法来保持持续的状态信息?


解决方案

是的,它有可能被杀死,如果用户离开了,而应用程序重新启动应用程序。你可能需要阅读流程和生命周期这部分。

您应该找到一个合适的地方保存状态到持久性存储。如果它只是几个整数和字符串,它应该是pretty简单将它们保存到共享preferences,因为他们改变。请参见数据存储 - 共享preferences

I use my Android app's application Context as a storage area for "current state" information for my app.

I'm finding that in the field, there are cases when this information goes away on some people's devices causing various NullPointerExceptions since I expect the data to be there when the app resumes and starts rebuilding the necessary activities.

This usually happens when the user hits "Home", does something else, then eventually wanders back into the app - it attempts to go back to where it was before, but the application Context has mysteriously lost all its previously-saved state information (in my case, a few integers and a few Strings).

I know this is a very vague question, but are there any cases (other than the user using "back" to back completely out of the application) where the application Context gets completely destroyed even though the application is not terminated?

Is there a better way to maintain persistent state information?

解决方案

Yes, it is possible for the Application to be killed and restarted if the user leaves the application for a while. You might want to read this section on processes and lifecycles.

You should find an appropriate place to save state to a persistent store. If its just a few integers and strings, it should be pretty simple to save them to shared preferences as they change. See data storage - shared preferences.

这篇关于请问Android应用程序上下文曾经被摧毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆