SharedPreferences are sometimes deleted

前端 未结 3 772
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题