Obsolete
Status Update
Comments
as...@google.com <as...@google.com> #2
This has been fixed on developer.android.com . See
http://developer.android.com/reference/android/content/SharedPreferences.html#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)
and
http://developer.android.com/guide/topics/ui/settings.html#Listening
and
Description
1) Make a new application
2) Make a new inner class or anonymous inner class to implement your OnSharedPreferenceChangeListener interface, since you don't want this to be part of the public interface of your object. lets calle this "MyListener"
3) Register MyListener like this;
getSharedPreferences(arg). registerOnSharedPreferenceChangeListener(new MyListener())
Expected: MyListener onSharedPreferenceChanged() should be invoked when preferences change
Actual: that may not happen
Since SharedPreferencesImpl stores the listener reference in a weak hash map, it will be collected at next GC time.
This is surprising to the caller, and it should be clearly documented that registerOnSharedPreferenceChangeListener() stores its listeners in a weak hash map and that the caller must retain references for the listener's expected lifetime.