当 Android 操作系统决定“暂时”关闭时如何不丢失类变量值我的类要释放内存吗?
Ddms 告诉我,当我回想起过去调用的类时,它执行的是 onCreate()
而不是我期望的 onResume()
... 我注意到在这种情况下我存储在类变量中的值丢失并且为空。 我认为 Android 决定这样做是为了释放内存资源(不是吗?)。 我知道我可以使用 Sharedpreferences 以持久的方式存储数据然后检索......但在我看来,这是一种非常肮脏的方式。
所以,我的问题是:如何在 Android 自动决定的 onDestroy()
(我认为?)之后保留变量的值?
Ddms tells that, when I recall my class called in the past, it performs an onCreate()
instead of onResume()
that I expected...
I noticed that values that I stored in variables of my class in this case are lost and are null.
I presume that Android decide to do so to free memory resources (isn't it?).
I know that I could use Sharedpreferences to store data in a persistent way and then retrieve... But this is a really dirty way, in my opinion.
So, my question: how to have variables' values preserved also after an onDestroy()
(I think?) that Android decided automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您没有可见的活动时,Android 将随时终止您的进程。例如,用户可能会进入“设置”并终止您的应用程序。
静态数据成员(我对“我的类的变量”描述的解释)充其量只是缓存。它们无法替代持久数据模型,无论您使用数据库、XML 文件、JSON 文件还是其他文件。
因此,如果您希望“保留变量的值”,请将它们保存在持久的地方。
Android will terminate your process at any time when you have no visible activities. For example, the user might go into Settings and terminate your app.
Static data members (my interpretation of your "variables of my class" description) are only meant to be caches, at best. They are no substitute for a persistent data model, whether you use a database, an XML file, a JSON file, or whatever.
So, if you want "variables' values preserved", save them someplace persistent.
您可能会发现此数据存储页面很有帮助。如果您的数据是原始数据,则推荐使用 SharedPreferences 路线。 (为什么你认为它们很脏?)如果你需要存储一个对象,你可以使用内部存储,如该页面上所述。
You might find this page on data storage helpful. If your data is primitive, SharedPreferences are the recommended route. (Why do you think they are dirty?) If you need to store an object, you can use internal storage, as documented on that page.
http://developer.android.com/reference /android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
如果您不喜欢 SharedPreferences,那么您可能需要查看 内容提供商 尽管内容提供程序跨应用程序共享数据,但它们还为您提供在 SQLlite 中存储持久数据以及仅您的应用程序可用的文件的功能。在这种情况下,即使关闭并重新启动应用程序后,以这种方式存储的数据也将可用。
If you don't like SharedPreferences, then you might want to look into Content Providers Even though Content Providers share data across applications, they also provide functionality for you to store persistent data in SQLlite and files that are available only to your app. In this case data stored in this fashion will be available even after closing and restarting your app.
您可以使用 Android 架构以及 onSaveInstanceState 和 onRetainNonConfigurationState 方法传递名称:值对或可序列化对象来保存动态状态。您可以通过写入首选项并执行数据库写入来保留其他答案中所解释的状态。
You can save dynamic state by passing name:value pairs or serializable objects using the Android Architecture and the methods onSaveInstanceState and onRetainNonConfigurationState. You can persist state as explained in the other answers by writing to prefs and doing database writes.
我一直在使用自定义应用程序类来存储应用程序生命周期内的数据。
如何在 Android 中声明全局变量?
I've been using custom Application class to store data over application life line.
How to declare global variables in Android?