Search code examples
androidandroid-preferences

Is it possible to have multiple activities listen with SharedPreferences.OnSharedPreferenceChangeListener


I have two activities, my MainActivity and my SettingsActivity (which instantiates the PreferenceFragment.

I would like both activities to do:

final SharedPreferences mPrefs = 
        getSharedPreferences(getString(R.string.shared_prefs), 
                             Context.MODE_PRIVATE);
mPrefs.registerOnSharedPreferenceChangeListener(mListener);

Where each has its own implementation of mListener (a SharedPreferences.OnSharedPreferenceChangeListener) .

What I am seeing is that only one of the of the OnSharedPreferenceChangeListener is triggered.

Any ideas?


Solution

  • Yes, it is possible to have multiple listeners at once as seen here:

    https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/SharedPreferencesImpl.java#186

    It is likely your problem is elsewhere.