Friday, 22 August 2014

How to access checkbox value in a settings activity from another activity?



I've created a new settings activity using the wizard in Eclipse. I would like to know how to access the value that reflects the user's choices. In particular, a checkbox option.


The code it has produced is as follows:


/src/com/example/myapp/SettingsActivity.java:



@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
private void setupSimplePreferencesScreen() {
addPreferencesFromResource(R.xml.pref_general);
//...
}


res/xml/pref_general.xml:



<CheckBoxPreference
android:defaultValue="true"
android:key="example_checkbox"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />


The wizard creates quite a large example, so I'm not sure if I've copied/pasted all the relevant code, but I have spent quite a long time and I'm pretty sure this is about it, (besides the string xml file).


How can I access the checkbox's value from another activity? I'm guessing I need to use a SharedPreferences object, but I don't know what keys I would use to access the data, as I can't find any constants in the code for that? Thanks


No comments:

Post a Comment