Lose some values using getStringSet and putStringSet

android, sharedpreferences

Solution

From my understanding, it's a bug. The problem is when you try to store a set, Android compares the modified set that you are trying to save with the current one stored in the SharedPreferences. And when they are the same object (having the same reference), the new one is ignored.

A work around is to create a new copy of the set before storing it.

Here is the details and the issue is submitted at https://code.google.com/p/android/issues/detail?id=27801.

Problem

I found out that some values had been stored to `Set` got lost after restarting the application. Why? Is this an Android API bug? Somebody advised to store strings in a database. I haven't tested it yet, but I think this advice is a solution of the problem. What about `getStringSet` and `putStringSet`?

Original source