SharedPreferences.getAll () does not have preference keys that are false. How to get all the keys?

Only CheckboxPreference keys with android: defaultValue = "true" will be displayed in the code below. Thus, none of the keys with defaultValue = "false" is displayed. How do I get a list of all the keys in my preferences file?

PreferenceManager.setDefaultValues(this,R.xml.settings,true); //readAgain=false is same result SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this); Map<String, ?> prefMap=prefs.getAll(); for(String key:prefMap.keySet()) { Log.d("preference key=",key); //only keys with true are shown } } 

According to the docs, getAll () should: "Extract all values ​​from settings." However, apparently, only keys that have a default value = true if the preference was not set by the user later.

EDIT: This is probably due to Android Bug: 6441 , but does not give me a workaround to get all the keys.

+6
source share
1 answer

You can access all settings as follows (I use it to enable / disable all settings):

  final ListAdapter adapter = getPreferenceScreen().getRootAdapter(); for (int i = 0; i < adapter.getCount(); i++) { Object object = adapter.getItem(i); if(object instanceof Preference){ ((Preference)object).setEnabled(state); // state -> my own variable } } 
0
source

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


All Articles