Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing preferences in SharedPreferences in Android, not just Values

from what I can incur out of the SharedPreferences documentation, I can update a preference, add one or clear all preference values in a shared preference file.

But I want to completely clear everything inside a shared preference file, not just the values, but the preferences they refer to as well.

like image 361
Orca Avatar asked Sep 02 '10 19:09

Orca


2 Answers

If you have a SharedPreferences.Editor object and you call clear(), does this not get you what you want? It will remove all preferences and if you call sharedPref.getAll() it should give you a map of size 0 [I just tested this].

To remove one specific preference, call editor.remove(pref), where pref is the preference name.

PS: Don't forget to commit your changes by calling commit() or apply() method on the editor. apply() is faster as it is asynchronous. commit() is synchronous but returns a boolean indicating if the commit succeeded.

like image 123
antonyt Avatar answered Oct 24 '22 01:10

antonyt


you could try deleteFile to delete the sharedpreferences file in your app's private storage.

If you just want to delete the contents but not the file, calling .edit().clear().commit() should do it.

If just you want to delete one preference, calling .edit().remove("key").commit() should work.

like image 26
QRohlf Avatar answered Oct 24 '22 01:10

QRohlf