Android: String set preference is not persistent

前端 未结 2 436
忘掉有多难
忘掉有多难 2020-12-11 09:00

I have a problem with storing string set preference. I have these utility methods for storing:

public static void putStringSet(SharedPreferences pref, Editor         


        
2条回答
  •  无人及你
    2020-12-11 09:35

    My condition is very similar to yours, the only difference is when restart the app, preference contains A, B, C, but when reinstall it or reboot the phone, B&C are gone.

    I also tried replace commit() with apply(), as this post adviced SharedPreferences not persistent , but still not to work.

    I solved this problem by remove & commit the preference before replacing it:

    editor.remove("StringSetKey");
    editor.commit();
    
    editor.putStringSet("StringSetKey", newSet);
    editor.commit();
    

    Ps: you can type adb pull /data/data//shared_prefs/xxxx.xml in cmd line to see if the commit() really works

    Pps: I think this is a bug with putStringSet....

    hope this will help you ;)

提交回复
热议问题