Shared Preferences get lost after shutting down device or killing the app

前端 未结 3 987
礼貌的吻别
礼貌的吻别 2020-12-06 05:57

there are lots of questions out there related to shared preferences and the alternatives. My problem: when I shut down the device or kill the app, the shared preferences get

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

    I was having the exact same issue when using string set with the SharedPreferences. I discovered why this was happening and how to resolve it.

    Very important to read the API documentation for SharedPreferences.getStringSet (String key, Set defValues)

    Essentially DON'T modify or touch the returned Set! If you do you will get odd results with the SharedPreferences. :)

    Enjoy!

    0 讨论(0)
  • 2020-12-06 06:42

    Your problem seems to be the same as discussed here for the device Samsung Galaxy S running standard Froyo 2.2.1. Some impressions from their discussion:

    The Log shows:

    01-19 12:08:56.856 W/ApplicationContext( 4563): Attempt to read preferences file /dbdata/databases/com.mobilemerit.ultimatefaves/ shared_prefs/com.mobilemerit.ultimatefaves_preferences.xml without permission

    One possible workaround seems to be:

    My user seem to have had the same symptom.

    He e-mailed me that he had to do a factory-reset to his phone due to other reasons, he re-installed the app from the market (something that didn't help before the reset), and now everything works as expected.

    Another workaround:

    I have the same problems with my 2.2.1 Galaxy S (rooted), I solve it by deleting my app folder (com.xxx) in \dbdata\databases. Does someone have the same problem on another phone ?

    And this guy actually found the cause of it:

    I installed my app (WidgetLocker) and it got app_81, then I uninstalled using "adb uninstall com.teslacoilsw.widgetlocker" then reinstalled and found my preferences had persisted across the uninstall, but no permission denied error. I uninstalled again in the same way, this time I went to the Market and installed Androidify. This is when things got interesting: (...)

    So Androidify got my uid and my preferences now are belong to them. (...)

    And we are screwed...

    There is a bug report for this issue: http://code.google.com/p/android/issues/detail?id=14359

    Google declined that bug because it actually is a bug caused by modifications done by the vendor for the device.

    The authors of the App "Beautiful Widgets" gives its users the following advice:

    ** SAMSUNG GALAXY S & ANDROID 2.2 **
    There is a bug in the Samsung Update which prevent to save the settings file. You need to factory reset your phone (...)

    So this should help.

    Please respond to this answer, if this happens to be the same issue on your Xoom device.

    0 讨论(0)
  • 2020-12-06 06:55

    I've figured out a solution that works both, on my Acer and on my XOOM device: you have to call clear() on the editor before committing new data:

    public void saveCollection(Context context){
        SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.restore_values), 0);
        SharedPreferences.Editor e = settings.edit();
        e.clear();
        e.putStringSet(context.getString(R.string.collection), collection);
        e.commit();
    }
    
    0 讨论(0)
提交回复
热议问题