简体   繁体   English

在Android中保存单身人士的状态?

[英]Saving the state of a singleton in Android?

I have a singleton data manager for a specific part of my application. 对于我的应用程序的特定部分,我有一个单例数据管理器。 This data manager holds the state of a multistep process between activities. 该数据管理器保持活动之间的多步骤过程的状态。 This works perfectly except when the application is placed into the background for an extended period of time as the data manager is cleared from memory. 除非将应用程序放置在后台很长时间(因为数据管理器已从内存中清除),否则此方法非常有效。 My question is what is the best way to save the state of a singleton that isn't part of one specific activity? 我的问题是,保存不属于某个特定活动的单例状态的最佳方法是什么? All activity state is saved using onSaveInstanceState callback, does Application receive a callback to save state? 所有活动状态都使用onSaveInstanceState回调保存,应用程序是否收到回调以保存状态?

您可以在每次状态更改时将状态保存在SharedPrefs中 ,并在每次初始化单例时从SharedPrefs中获取状态。

I have had some similar experiences and the easiest one, straight out of my head, would be either to use the onSaveInstanceState or, persist the whole thing locally in SQLite or perhaps in the SharedPreferences , even if that might not be the right approach for this task. 我也有类似的经历,最简单的方法就是直接使用onSaveInstanceState或者将整个事情本地保留在SQLiteSharedPreferences ,即使这可能不是正确的方法任务。

If you want I can provide you with some sample code for either of the options, otherwise please check the following: 如果您愿意,我可以为您提供一些选项的示例代码,否则请检查以下内容:

SharedPreferences : http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html 共享的首选项: http ://www.vogella.com/articles/AndroidFileBasedPersistence/article.html

SQLite: http://www.vogella.com/articles/AndroidSQLite/article.html SQLite: http//www.vogella.com/articles/AndroidSQLite/article.html

Saving and recieving instance state: How to use onSavedInstanceState example please 保存和接收实例状态: 请使用onSavedInstanceState示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM