SharedPreferences are sometimes deleted

前端 未结 3 759
名媛妹妹
名媛妹妹 2020-12-29 05:48

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. M

相关标签:
3条回答
  • 2020-12-29 06:14

    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.

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

    0 讨论(0)
  • 2020-12-29 06:15

    SharedPreferences usually work great and as you mentioned without any problem on smartphones. It should also work on tablets. However it would be best to use sqlite or backing your shared prefs on a server for each users id data loss is a problem.

    0 讨论(0)
  • 2020-12-29 06:24

    I have had the same issue. All values from shared preferences become default if one value occasionally written with key = null. This is possible when you creating new constants using Eclipse, which inits them with null by default, and then you forgot to go to the beginning of the class and change constant to some meaningful value. If you continue using the app, read operation of shared preferences will return valid values. But when context is changed(for example, when activity killed by android and recreated) all values from shared preferences will return default values(zeroes).

    So check your shared preferences's keys! I hope this answer will help someone.

    0 讨论(0)
提交回复
热议问题