Set in android sharedpreferences does not save on force close

后端 未结 2 1390
梦如初夏
梦如初夏 2020-12-15 18:15

Im trying to use androids sharedpreferences, I´ve logged everything and the code below really commits the string set. The problem is when I force close the app and start aga

2条回答
  •  醉话见心
    2020-12-15 18:48

    Take a look here.

    Also for refference:

    SharedPreferences

    SharedPreferences.Editor

    EDIT:

    There's actually a bug with this one, see here. An extract from there:

    This problem is still present on the 17 API level.

    It is caused because the getStringSet() method of the SharedPreferences class doesn't return a copy of the Set object: it returns the entire object and, when you add new elements to it, the commitToMemory method of the SharedPrefencesImpl.EditorImpl class see that the existing value is equal to the previous one stored.

    The ways to workaround this issue is to make a copy of the Set returned by SharedPreferences.getStringSet or force the write to memory using other preference that always change (for example, a property that stores the size of the set each time)

    EDIT2:

    There might be a solution here, take a look.

提交回复
热议问题