Reputation: 14798
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
Upvotes: 1
Views: 526
Reputation: 1457
PreferenceManager.getDefaultSharedPreferences(context).
getBoolean(example_checkbox, defaultValue);
The key you use is the one you sepcified in the android:key
field
Upvotes: 3