Search code examples
androidandroid-fragmentsandroid-preferencesonpreferenceclicklistener

Android PrefernceFragment implements OnPreferenceClickListener never invoked on clicks


I'm trying to implement OnPreferenceClickListener over PrefernceFragment and it seems like the onPreferenceClick() is never called.
I have another PrefernceFragment implementing OnSharedPreferenceChangeListener and it works just fine.
Is this a bug of the OS ? is the OnPreferenceClickListener not supposed to be supported for PrefernceFragment ?
If there is no need to register the fragment as listener as I read, then I really think my code is correct.

    public class myClass extends PreferenceFragment implements OnPreferenceClickListener {

    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(com.XX.ZZ.R.xml.YY);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        // never called.
    }
}

Solution

  • PreferenceFragment doesn't have a onPreferenceClick() method. There is really no need to listen for click events since the Android fragment takes care of writing the preference values into memory. If you really need to watch for a click event, you can use findPreference(CharSequence key) to find each of the Preferences you want to watch and then call setOnPreferenceClickListener(this) on those.