atpanos
atpanos

Reputation: 473

How to edit SharedPreferences so that OnSharedPreferenceChanged is called

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.

Upvotes: 1

Views: 438

Answers (1)

Stefan
Stefan

Reputation: 4705

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());
    }
}

Upvotes: 2

Related Questions