How can I force SharedPreferences to save?

tommitak

New Member
As far as I can tell, values aren't stored in Android's \[code\]SharedPreferences\[/code\] until they're explicitly accessed. That is, while they may have default values in XML, no value is placed in a \[code\]SharedPreferences\[/code\] store until an accessor method is called, which is why all the accessors have "default" parameters included.While this isn't a huge deal for simply pulling values out of the preference store, it prevents any attempt to get all the preference keys that are used in the application, even if they are stored in XML. The keys don't appear when \[code\]SharedPreferences#getAll()\[/code\] is called unless the preference has already been explicitly accessed.Is there any way to force all preferences defined in XML to be saved into a \[code\]SharedPreferences\[/code\] store? The nearest solution I can find is to manually parse the Preference XML files, find all keys and defaults, and save the default value for each one. Is there a cleaner approach?UPDATEAfter looking at this in more depth, I've been getting a partial list of preferences for a different reason. When the defaults are set, only \[code\]EditTextPreference\[/code\] and \[code\]ListPreference\[/code\] values are saved. The other two, a custom preference and a \[code\]CheckBoxPreference\[/code\], are completely ignored. Here's an example of the \[code\]CheckBoxPreference\[/code\] that's being ignored: \[code\]<CheckBoxPreference android:defaultValue="http://stackoverflow.com/questions/10708309/false" android:key="PREF_NAME" android:summary="Summary text" android:title="Title" />\[/code\]Any idea why not all defaults are being set?
 
Back
Top