Xamarin android OnSharedPreferenceChangeListener

I have this action and the problem with OnSharedPreferenceChanged is not being called. My use case is that I want to show the meaning of preference in the description of preferences. The code below translated is translated from java, where it works fine.

[Activity] public class PrefActivity : PreferenceActivity, ISharedPreferencesOnSharedPreferenceChangeListener { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); AddPreferencesFromResource(Resource.Xml.preferences); } protected override void OnResume() { base.OnResume(); PreferenceScreen.SharedPreferences. RegisterOnSharedPreferenceChangeListener(this); } protected override void OnPause() { base.OnPause(); PreferenceScreen.SharedPreferences. UnregisterOnSharedPreferenceChangeListener(this); } #region ISharedPreferencesOnSharedPreferenceChangeListener implementation public void OnSharedPreferenceChanged(ISharedPreferences sharedPreferences, string key) { Preference pref = FindPreference(key); if (pref is ListPreference) { ListPreference listPref = (ListPreference)pref; listPref.Summary = listPref.Entry; } } #endregion } 

Iam, using the Xamarin.Android v4.6.8 code above, is my last attempt to make this working environment also an attempt to use the PreferenceScreen.PreferenceChange event to handle preference changes, but without any results.

Tanks for reference.

+4
source share
1 answer

I have found a solution! the change

 PreferenceScreen.SharedPreferences. RegisterOnSharedPreferenceChangeListener(this); 

to

 PreferenceManager.GetDefaultSharedPreferences(this). RegisterOnSharedPreferenceChangeListener(this); 

will do the trick.

I hope this helps someone.

+6
source

Source: https://habr.com/ru/post/1489538/


All Articles