0

I have a CheckBoxPreference defined as follows:

<CheckBoxPreference
    android:defaultValue="true"
    android:key="prefVisible"
    android:summary="@string/pref_visible_summary"
    android:title="@string/pref_visible" >
</CheckBoxPreference>

My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.

Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?

Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true

This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.

Jan Tacci
  • 3,131
  • 16
  • 63
  • 83

1 Answers1

1

No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • So in my main activity's onCreate() method just call PrefereceManager.setDefaultValues? – Jan Tacci Mar 18 '13 at 23:48
  • Curious, why do the getter methods have default values if the UI definitions (xml) have them too? – Jan Tacci Mar 18 '13 at 23:50
  • Yes, but be careful with the flag param. I forgot what it is that calling it again will not reset the preferences. – Hoan Nguyen Mar 18 '13 at 23:50
  • What do you mean by "flag param?" – Jan Tacci Mar 18 '13 at 23:51
  • The setDefautValues read the default you set in the XML and set them. – Hoan Nguyen Mar 18 '13 at 23:51
  • setDefaultValues have 3 parameters, set the last param to false. – Hoan Nguyen Mar 18 '13 at 23:53
  • I need to read a bit more about all this but let me see if I can understand by asking this question: Are preference default values in general provided in the Xml and then set upon calling setDefaultValues() or is it better to *hard code* the default value in the getter method for the preference? – Jan Tacci Mar 18 '13 at 23:55
  • I think it is better to set the default value in the XML, that make it easy to be portable. I do not understand what do you mean by getter method for the preference. – Hoan Nguyen Mar 18 '13 at 23:59
  • At the bottom of my initial question I provided my *workaround* which was calling the following method: sharedPrefs.getBoolean("prefVisible", true) getBoolean being the getter method and true being the second argument. – Jan Tacci Mar 19 '13 at 00:01
  • I still think setting in the xml is better. You may decide that it is better to change the default value, thus if you change it in the xml and call setDefaultValues your code still work without changing the getter. – Hoan Nguyen Mar 19 '13 at 00:06