强制关闭后共享preferences重置 [英] SharedPreferences being reset after force close

查看:165
本文介绍了强制关闭后共享preferences重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够成功地实现共享preferences到我的应用程序,但我已经遇到了与数据重置/如果我杀了通过任务管理器应用程序中删除的问题。

I have been able to successfully implement Shared Preferences into my application but I have ran into a problem with the data being reset/deleted if I kill the application through a task manager.

我使用的是静态方法保存,这样,我只需要方法一次,并且可以在我的应用程序无处不在调用它。

I am using a static method for saving, that way I only need the method once and can call it everywhere within my app.

protected static synchronized void save(Context cntx){
    SharedPreferences preferences2 = cntx.getSharedPreferences("BluRealms", 0);
    SharedPreferences.Editor editor = preferences2.edit();
    editor.putBoolean("level", Stats.level);
    editor.commit();
}

只要我杀了我的应用程序所有的数据被重新设置为我的共享preferences默认设置保存方法。

As soon as I kill my app all of my data gets set back to the default settings in my SharedPreferences save method.

我也做了一些搜索,发现了几个职位,说将机器人:执着=真到清单文件将解决这个问题,但该数据仍与该重置甚至

I also did some searching and found a few posts that say adding android:persistent="true" into the of the manifest file would fix the problem, yet the data is still reset even with this.

编辑:嗯,我想我找到了一点信息,我的问题。本期突出了与三星Galaxy S手机不保存共享preferences正常,这是我测试的设备有问题。 HTTP://$c$c.google.com/p ?/安卓/问题/细节ID = 14359 - 尤其是注释6

Well I think I found a bit of information on my problem. This Issue highlights a problem with Samsung Galaxy S phones not saving SharedPreferences properly, which is the device I am testing on. http://code.google.com/p/android/issues/detail?id=14359 - especially comment 6

在此更多的信息将是巨大的!

Any more information on this would be great!

推荐答案

好吧我能够从我的保存方法删除受保护的静态来解决这个问题。

Ok I was able to solve this by removing the "protected static" from my save method.

,我只是摆在每个类的保存方法,将需要保存,然后只调用保存的方法在的onPause()和的onDestroy()方法。

Instead of calling a global save method, I simply placed the save method in each class that would need to save and then only call the save method in the onPause() and onDestroy() methods.

我发现,如果我叫保存()一类似乎也抹去我的共享preferences当我关闭了应用程序内的次数太多。

I noticed that if I called save() too many times within a class that also seemed to erase my SharedPreferences when I closed the app.

提示:

不要使用静态方法来获取或设置共享preferences

Do not use static methods for getting or setting shared preferences

这篇关于强制关闭后共享preferences重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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