The best place according to the Android dock is:
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
And you must store the listener in a field variable (or use the Activity object itself - as in the above source code) so that it does not collect garbage.
those. An anonymous class object cannot be used as OnSharedPreferenceChangeListener.
source
share