How to get all keys in SharedPreferences
, not the value of the preference just key only?
prefA = getSharedPreferences("MyAttack", MODE_PRIVATE);
prefB= getSharedPreferences("MySkill", MODE_PRIVATE);
How to get all keys in SharedPreferences
, not the value of the preference just key only?
prefA = getSharedPreferences("MyAttack", MODE_PRIVATE);
prefB= getSharedPreferences("MySkill", MODE_PRIVATE);
Kotlin will allow you to get all your SharedPreferences keys with just one line by using Map.
Cheers mate
Check out the below code for
getAll()
methodAlthough @Blackbelt's answer is quite popular here, I think it is not actually targeting the question. (It is not suprising since the question mixes up the terminology of preferences names and keys.) I guess the question is how to find out which shared preferences instances have been created - which can be of interest if the names are created dynamically.
Here are two strategies for that:
create another shared preferences "meta" instance where all created shared prefences instances are registered by adding a key/value pair for it to the meta prefs - with the key being the shared prefences name and the value being any value e.g.
true
.To obtain all shared prefences created by you, use the meta prefs and follow the answer of @Blackbelt.
shared preferences have a backup file, which is stored in folder
/data/data/YOUR_PACKAGE_NAME/shared_prefs
with nameYOUR_PREFS_NAME.xml
So you can look into that directory for your shared preferences files. But be careful, there might be shared preferences files that were not created by your logic! Therfore I would stick with the first approach.Use the getAll() method of
android.content.SharedPreferences
.What you can do is use getAll() method of SharedPreferences and get all the values in Map and then you can easily iterate through them:
For more, you can check PrefUtil.java's dump() implementation with this link.
SharedPreferences
has the methodgetAll()
that returns aMap<String, ?>
. From the Map you can retrieve easily the keys withkeySet()
and the key/value mappings withentrySet()
: