Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This "problem" is documented on <a href="http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29"><code>SharedPreferences.getStringSet</code></a>.</p> <p>The <code>SharedPreferences.getStringSet</code> returns a reference of the stored HashSet object inside the <code>SharedPreferences</code>. When you add elements to this object, they are added in fact inside the <code>SharedPreferences</code>.</p> <p>That is ok, but the problem comes when you try to store it: Android compares the modified HashSet that you are trying to save using <code>SharedPreferences.Editor.putStringSet</code> with the current one stored on the <code>SharedPreference</code>, and both are the same object!!!</p> <p>A possible solution is to make a copy of the <code>Set&lt;String&gt;</code> returned by the <code>SharedPreferences</code> object:</p> <pre><code>Set&lt;String&gt; s = new HashSet&lt;String&gt;(sharedPrefs.getStringSet("key", new HashSet&lt;String&gt;())); </code></pre> <p>That makes <code>s</code> a different object, and the strings added to <code>s</code> will not be added to the set stored inside the <code>SharedPreferences</code>.</p> <p>Other workaround that will work is to use the same <code>SharedPreferences.Editor</code> transaction to store another simpler preference (like an integer or boolean), the only thing you need is to force that the stored value are different on each transaction (for example, you could store the string set size). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload