1

I have a PreferenceScreen in which I have a CheckBox Preference and a simple Preference, which calls a custom dialog. In this custom dialog the user can pick a number. By pressing OK in this dialog I want to save the number by editing my sharedPreferences. After saving it should call automaticaly the OnSharedPreferenceChanged method.

The CheckBox Preference works fine.

DIF
  • 2,470
  • 6
  • 35
  • 49
atpanos
  • 473
  • 6
  • 9
  • I wonder if you have tried http://stackoverflow.com/questions/3799038/onsharedpreferencechanged-not-fired-if-change-occurs-in-separate-activity – Siddharth Feb 13 '12 at 16:38
  • I dont understand what you want to show me here... I am registering and unregistering the onchangelistener and it works, I know it because it works with the CheckBox Preference. After picking the number in the dialog I want to save the number in the SharedPreferences and saving a number should automaticaly call the OnSharedPreferenceChanged method. I dont know if this is even possible... – atpanos Feb 13 '12 at 16:47

1 Answers1

2

Your custom dialog should extend DialogPreference. If the preference has been modified call persistXXX() (XXX being the name of the type) like in this example:

@Override
public void onDialogClosed(final boolean positiveResult) {
    if( positiveResult && this.isPersistent() ) {
        final StringBuilder b = new StringBuilder();
        b.append(this.hour).append(':');
        if( this.minute < 10 )
            b.append('0');
        b.append(this.minute);
        this.persistString(b.toString());
    }
}
Stefan
  • 4,645
  • 1
  • 19
  • 35