vedavis
vedavis

Reputation: 968

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 ?

Upvotes: 1

Views: 1151

Answers (2)

dsalaj
dsalaj

Reputation: 3207

The accepted answer did not work for me. Might be also due to my setup since I use a PreferenceFragmentCompat instead of PreferenceActivity etc. Digging through the source code of Preference lead to a very simple solution:

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if(positiveResult) {
        persistString(s);
        callChangeListener(s);  // the solution!
    }
}

or in my case (Button click, Kotlin):

val button = holder?.findViewById(R.id.my_button)
button?.setOnClickListener {
    persistString(s)
    callChangeListener(s)  // the solution!
}

Upvotes: 1

Ramesh Sangili
Ramesh Sangili

Reputation: 1631

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

Upvotes: 1

Related Questions