Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerOnSharedPreferenceChangeListener not working for changes made in different process [duplicate]

I have registered registerOnSharedPreferenceChangeListener with my SharedPreferences in my Activity. When I change the data saved with SharedPreferences inside service running in different process, onSharedPreferenceChanged is not called!

What should I do to make android call onSharedPreferenceChanged?

like image 577
Sushant Kr Avatar asked Apr 14 '12 02:04

Sushant Kr


1 Answers

Most likely, it is being garbage collected by gc. I have faced this problem before. Make a global variable which keeps a hard reference to the listener.

  OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
      public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // Implementation
      }
    };

The answer at SharedPreferences.onSharedPreferenceChangeListener not being called consistently explains it best

like image 121
Akhil Avatar answered Oct 27 '22 01:10

Akhil