Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all but one SharedPreference

I want to clear all but one SharedPreference. If I've saved 10+, is there a better way then removing each individually? This gets a bit redundant:

preferences.edit().remove("1").commit();
preferences.edit().remove("2").commit();
preferences.edit().remove("3").commit();
...
preferences.edit().remove("15").commit();
like image 518
ono Avatar asked Apr 30 '14 18:04

ono


2 Answers

You could also get the value you want to keep, clear(), and re-add it before commiting.

like image 124
matiash Avatar answered Oct 14 '22 08:10

matiash


you can loop through all the keys

Map<String,?> prefs = pref.getAll();
for(Map.Entry<String,?> prefToReset : prefs.entrySet()){
    edit.remove(prefToReset.getKey()).commit();
}

then skip over the key/s you dont want to remove

like image 31
tyczj Avatar answered Oct 14 '22 08:10

tyczj