开发者

SharedPreferences not refreshing?

开发者 https://www.devze.com 2023-02-10 07:43 出处:网络
I\'m using sharedPreferences to store some simple data that I access periodically. I\'ve noticed that when I \"put\" something using the SharedPreferences.Editor (I do call commit()) when开发者_开发百

I'm using sharedPreferences to store some simple data that I access periodically. I've noticed that when I "put" something using the SharedPreferences.Editor (I do call commit()) when开发者_开发百科 I later try to access the prefs with prefs.getAll() my newly added item is not there.

Oddly, if I close my app and fire it up again, it appears. It's like the prefs are not refreshing while my app is running. Is that by design? What gives?

BTW, I've noticed the same behavior while doing editor.remove("key"). I remove something (and call commit()), and when I call getAll() the deleted item is still in the Map returned. If I try to delete it again, my app force closes.

I must be doing something wrong. Any help is appreciated.

Thanks, Bobby


This should not happen if you use SharedPreferences the right way. The only two scenarios I can think of when this happens is if:

  1. commit() returns false (yes, it does really have a return value) or
  2. you are trying to use the SharedPreferences across multiple processes, which is not supported yet.


Ok, my blunder. I was displaying the list of prefs in a freaking dialog. I missed that the dialog create was only called once so it never updated with my new values. Gah!

Thanks anyway!

0

精彩评论

暂无评论...
验证码 换一张
取 消