Search code examples
androidandroid-preferences

Custom DialogPreference does not trigger OnPreferenceChanged in PreferenceActivity


The custom DialogPreference that gets called from a PreferenceActivity does not call OnPreferenceChange() when the dialog is dismissed.

I thought that persistXXXX() was the trigger:

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if(positiveResult) {
        persistString(s);
    }
}

but onPreferenceChanged() does not get called.

All other standard preference types work (e.g. EditTextPreference, ListPreference, etc.).

So the question is: what triggers the OnPreferenceChange() for this preference type ?


Solution

  • protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(this);
            sp.registerOnSharedPreferenceChangeListener(this);
        }
    

    I think, you are missing registerOnSharedPreferenceChangeListener