I'm trying to create a settings section for my app. I have a res/xml/settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="@array/numberOfYears"
android:entryValues="@array/numberOfYears"
android:dialogTitle="How Many Years?"
/>
<CheckBoxPreference android:key="ignoreDates"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
/>
</PreferenceScreen>
I have a class called Preferences which extends PreferenceActivity and contains the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
I wasn't sure if this was required but I also added the following to my main activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, PREFS_MENU, Menu.NONE, "Preferences");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "Menu Item: " + item.getItemId());
switch (item.getItemId()) {
case PREFS_MENU :
startActivity(new Intent(this, Preferences.class));
return true;
}
return false;
}
This seems to work in that I get a "Preferences" menu which, when selected, launches my preferences. However, when I select anything the values don't seem to stick. My understanding was that the android:key was enough for the system to set the chosen values in the default shared preferences - is this not the case?
What am I missing?