Zylwee
Zylwee

Reputation: 1

OnSharedPreferenceChangeListener not called

here is my code in my main activity, never called, changes are done in another Activity, does it matter ?

SharedPreferences.OnSharedPreferenceChangeListener listener = new
  SharedPreferences.OnSharedPreferenceChangeListener() {

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
    {
        Toast.makeText(getBaseContext(), "clef modifiee : " + key,
            Toast.LENGTH_LONG).show();
        if(key == "LibraryName" || key == "LibraryPath")
        {
            LoadLibFile();
        }
    }
};

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(listener);

sorry for indent, on a tablet.

Thanks you

Upvotes: 0

Views: 912

Answers (1)

VendettaDroid
VendettaDroid

Reputation: 3111

Yes, It matters, once you are in other activity (which is not main activity). Your shared preference change listener is no more valid and will not be called. You can probably implement one listener in the activity where you are changing the shared preferences.

Upvotes: 2

Related Questions