Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through all keys of shared preferences?

SharedPreferences have method getAll, but it returns no entries despite the fact some keys exist:

PreferenceManager.getDefaultSharedPreferences(this).contains("addNewAddress"); 

returns true

Map<String, ?> keys=PreferenceManager.getDefaultSharedPreferences(this).getAll(); 

returns empty map

What is wrong? How to get list of all shared preferences?

like image 961
Eugene Chumak Avatar asked Feb 16 '12 11:02

Eugene Chumak


People also ask

Can shared preferences be hacked?

sharedPreferences arent safe.. sharedPreferences should just store config/setting-data not encrypted.. Quoting your link: "This essentially obfuscates the key so that it's not readily visible to attackers. However, a skilled attacker would be able to easily see around this strategy.

How does shared preferences store key-value pairs?

Have each key and value be separate entries in the SharedPreferences . After all, SharedPreferences is a key-value store. Use a dedicated preference file for your keys and values (so as not to collide with anything else you might want to store), then you know that all the keys are ones from the user.

Which method is used to retrieve data in shared preferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

What are the modes of shared pref?

Shared Preferences provide modes of storing the data (private mode and public mode). It is for backward compatibility- use only MODE_PRIVATE to be secure. This method takes two arguments, the first being the name of the SharedPreference(SP) file and the other is the context mode that we want to store our file in.


2 Answers

What you can do is use getAll() method of SharedPreferences and get all the values in Map<String,?> and then you can easily iterate through.

Map<String,?> keys = prefs.getAll();  for(Map.Entry<String,?> entry : keys.entrySet()){             Log.d("map values",entry.getKey() + ": " +                                     entry.getValue().toString());              } 

For more you can check PrefUtil.java's dump() implementation.

like image 78
Lalit Poptani Avatar answered Oct 10 '22 13:10

Lalit Poptani


i think the question has more to do with why

    PreferenceManager.getDefaultSharedPreferences(this).getAll() 

is returning an empty/contradictory map than with how to iterate over a standard java map. the android doc isn't really crystal clear about what's going here but basically it seems like the first call ever to

    PreferenceManager.setDefaultValues(this, R.xml.preferences,false) 

-- which is what you're supposed to call to initialize preferences when you start your app -- creates some kind of cached version of your preferences which causes future changes to your xml preferences file to be inconsistently handled, i.e., causing the mismatch you described in your question.

to reset this "cached entity", follow these steps (which you can sort of come up with from the above link):

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);     prefs.edit().clear();     PreferenceManager.setDefaultValues(this, R.xml.preferences, true); 
like image 39
rmanna Avatar answered Oct 10 '22 14:10

rmanna