0

I am trying to save my StringSet "purchasedAdvances" to the SharedPreferences of my app and noticed that it only gets written to the file, if I write another change to the file. Is this a bug or a feature anyone knows? Took me a while to figure this out...

so I am doing this in my OnSave():

for (String name: purchasedAdvances) {
            Log.v("Button", "inSave: " + name);
        }
savedCards = this.getActivity().getSharedPreferences(PURCHASED, Context.MODE_PRIVATE );
SharedPreferences.Editor editorCards = savedCards.edit();
editorCards.putInt("saved", purchasedAdvances.size());
editorCards.putStringSet(PURCHASED, purchasedAdvances);
editorCards.commit();

which works, but if I remove the line

editorCards.putInt("saved", purchasedAdvances.size());

because I do not need it, my Set does NOT get saved to the file and still has its old data. The Log lines above show me that there are new entries in the Set if I add any, but they do not end up in the pref file, which seems odd to me.

In the end, I can live with this, but still wondering if this is a bug or a feature or (which seems more likely) I am doing something terribly wrong and are not aware of it.

Tupsi
  • 33
  • 6
  • 1
    You must pass a new, separate instance of the `Set` in the `putStringSet()` call. You cannot use the instance returned from `getStringSet()`. https://stackoverflow.com/q/14034803 – Mike M. Apr 16 '22 at 14:51
  • 1
    nice explanation there, thanks @MikeM. for the reference. Seems I searched with the wrong terms earlier because that never showed up. – Tupsi Apr 16 '22 at 16:08

0 Answers0