Search code examples
androidandroid-preferencespreferenceactivity

Dialog Preferences from my PreferenceActivity aren't being saved


I have a bunch of EditTextPreferences and a TimePickerPrefernce(that I created) along with one CheckBoxPreference and none of the EditTextPreference or TimePickerPreference get saved, but the CheckBox does. I thought it might have been because of the bare minimum I have in my PreferenceActivity subclass, but seeing as how the checkbox gets saved I'm not sure if that is.

I've searched all over and everything seems to say that preferences should be automatically saved, and I can't find out any reason why they wouldn't be.

Here is my preference xml file:

<PreferenceCategory android:title="@string/settings_general_header" >
    <EditTextPreference
        android:defaultValue="15"
        android:summary="@string/settings_time_to_sleep"
        android:title="@string/settings_time_to_sleep_title" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="90"
        android:summary="@string/settings_sleep_cycle_time_summary"
        android:title="@string/settings_sleep_cycle_minutes" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="5"
        android:summary="@string/how_many_results"
        android:title="@string/settings_results_to_show_title" 
        android:numeric="integer" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/wakeupat_settings_header" >
    <CheckBoxPreference
        android:key="@string/save_wakeupat_time"
        android:title="@string/save_wakeupat_time" android:defaultValue="false"/>

    <TimePickerPreference
        android:dependency="@string/save_wakeupat_time"
        android:title="@string/wakeupat_preferred_time" />
</PreferenceCategory>

and here is my PreferenceActivity subclass:

package com.krej.timeforbed;

import java.util.List;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Button;

public class Settings extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
    }

}

Solution

  • you need to define android:key in all your controls (EditTextPreference and TimePickerPreference) to let the framework save values automatically.

    And since you have defined it in CheckBoxPreference that's why its saving it automatically.