Android: String set preference is not persistent

别等时光非礼了梦想. 提交于 2019-11-28 12:49:22
Mr_and_Mrs_D

This has a ridiculous amount of duplicates - I bet that you do :

set = prefs.getStringSet("X", new HashSet<String>());
set.add("yada yada");
prefs.putStringSet("X", set);

In short android sees that set and the one inside refer to the same set and does nothing. Correct ?

See: Misbehavior when trying to store a string set using SharedPreferences

zhangxaochen

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/<packagename>/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 ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!