androidsharedpreferencesandroid-sharedpreferences

Shared Preferences not saving StringSet when application is killed (it's a feature)


I've been loading and saving a Set<String> to the Android's SharedPreferences and it seemed to be working alright until I tested killing the application and realized the string set is not saved.

Set<String> stringSet = sharedPreferences.getStringSet(Constants.PREF_SHOULD_LOAD_SET, null);
if (stringSet != null) {
    if (stringSet.contains(data)) {
        stringSet.remove(data);
    } else {
        stringSet.add(data);
    }
    ...
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putStringSet(Constants.PREF_SHOULD_LOAD_SET, stringSet);
    editor.apply();
}

Some things I've tried:


Solution

  • After some unsuccessful searches I've found this - someone else had the same issue.
    He solved this by removing the value and adding it again.
    Further reading of the comments to his post revealed the cause, this is sharedPreferences.getStringSet documentation:

    Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

    So I took a slightly different approach and created a new set as follows:

    if (loadSubSet != null) {
        loadSubSet = new HashSet<>(loadSubSet);
    ...