SharedPreferences reset: map size is not 0 after reset

I am trying to remove my SharedPreferences , but it does not work: size not set to 0, as I expected.

 SharedPreferences sp = context.getSharedPreferences(name, mode); SharedPreferences.Editor e = sp.edit(); e.clear(); e.commit(); Map<String, ?> map = sp.getAll(); int size = map.size(); 

Any suggestions?

 private static String name = "ABC_PREFS"; private static int mode = Context.MODE_PRIVATE; 
+4
source share
3 answers

Your code looks great while reading it. Are you sure the context variable is initialized correctly? Are other SharedPreferences variables pointing to the same file possible?

If this is not a problem, consider accepting an application with a minimal sample and pasting this code to see it still fails. It's easier to solve a problem like this with a full application than with just a piece of code.

+1
source

SharedPreferences.Editor.clear () just removes only the values ​​of your preferences, which are not enough for you? You can delete all of your entries with SharedPreferences.Editor.remove () :

  for (String key: sp.getAll().keySet()) { e.remove(key); } e.commit(); 
0
source

You can write the size of the card in the general settings:

 e.clear(); e.putInt("size", map.size()); e.commit(); 

To get the size of the card call:

 int size = sp.getInt("size", 0); 
-2
source

Source: https://habr.com/ru/post/1446961/


All Articles