共享preferences有时会被删除 [英] SharedPreferences are sometimes deleted

查看:273
本文介绍了共享preferences有时会被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用的共享preferences(带的preferenceActivity的帮助下)来存储所有设置。但有时所有的设置都将被删除,所有值都重新设置为默认值。大多在Android 3.x的标签。 在普通智能手机没有任何问题。

My app uses SharedPreferences (with the help of a PreferenceActivity) to store all settings. But sometimes all settings are deleted and all values are set back to default. Mostly on Android 3.x tabs. On normal smartphones there are no problems.

我观察到,当应用程序被安卓杀死了所有的设置都将被删除。 我注意到这一点,因为不再显示该通知图标。 我也想知道,为什么进程被终止平板电脑,而不是智能手机。智能手机上的通知图标永远不会消失。

I have observed that all settings are deleted when the application process is killed by Android. I noticed this because the notification icon is no longer displayed. I also wonder why the process is killed on tablets and not on smartphones. On my smartphone the notification icon never disappears.

我的应用程序也有一个服务,主要工艺和服务的读取和写入设置为共享preferences。 我有时会发现,所有的设置将被清除各服一次写入共享preferences。但它不会发生的每一次。

My app also has a service, the main process and the service both read and write settings to the SharedPreferences. I've sometimes noticed that all settings are cleared once the service writes to the SharedPreferences. But it does not happen every time.

任何想法? 我意识到,共享preferences不应该使用不同的过程,但它适用于所有的智能手机没有问题!

Any ideas? I realize that the SharedPreferences should not be used by different processes, but it works on all smartphones without problems!

推荐答案

我找到了原因设置消失的原因: 如果两个进程将数据写入到共享preferences,它可能发生的所有共享preferences重置为默认值。 在我的情况下,所有的设置都被清除时,该服务将第一个数据共享preferences。

I found out the reason why the settings disappear: If two processes write data to SharedPreferences, it can happen that all SharedPreferences are reset to default values. In my case all settings are cleared when the service write the first data to SharedPreferences.

现在,该服务提供只读访问共享preferences。是由两个过程写入访问的设置存储在数据库中。

Now, the service provides only read access to the SharedPreferences. The settings that are accessed by both processes writing are stored in a database.

这篇关于共享preferences有时会被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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