Android Shared Preferences String Set
Android Shared Preferences String Set
Don’t stare at your code for too long.

A Nightmare with Shared Preferences and StringSet

mSharedPreferences.editor().putString(“Key”,”value”).apply();
mSharedPreferences.getString("key","defaultValue");
Set<String> sets = new HashSet<>();
sets.add("notification title 1");
mSharedPreferences.edit().putStringSet("keySet",sets).apply();//And to add another local notification:sets = mSharedPreferences.getStringSet("keySet",new HashSet<String>());sets.add("notification title 2");mSharedPreferences.edit().putStringSet("keySet",sets).apply();

Workaround

Set<String> newSet = new HashSet<String>(mSharedPrefs.getStringSet("keySet", new HashSet<String>()));

Pouring thoughts in technology and code. Writer with over 2M views. An Android and iOS developer by the day. Investor. Sometimes funny. linktr.ee/anupamchugh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store