Android Computer / Informatik

Android OnSharedPreferenceChangeListener not called

Hey everyone,

i now had a problem with an OnSharedPreferenceChangeListener multiple times. The problem was: it just never got called. I had a PreferenceActivity to manage the settings and i wanted to get notified in my main Activity when something changed. But my listener never was called.

Recently i found the answer to that problem on StackOverflow. The thing is that the SharedPreference keeps its listeners in a WeakHashMap. Which leads to the inability to use a nested inner class a the listener since it will be garbage collected.

So the solution to that problem is just storing the listener in a private variable like this: [code language=“java“]private OnSharedPreferenceChangeListener prefListener;[/code]

And everything will work just fine 🙂

Gr33tz Goddchen


4 Antworten auf „Android OnSharedPreferenceChangeListener not called“

THANK YOU! It’s really dumb that that thing gets garbage collected. Seems kind of important.

Thanks for the english too– I never would have found this otherwise.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.