1

In my Main activity I would like to listen to changes made in the Preferences Activity. I use

    SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences.OnSharedPreferenceChangeListener listener =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            //implement here -> but this peace of code is never called
        }
    };
    prefs.registerOnSharedPreferenceChangeListener(listener);

But the listener is never called. In my SettingsActivity I use the generated code from the Android Studio Template. For instance

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        setHasOptionsMenu(true);
        bindPreferenceSummaryToValue(findPreference("my_list"));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

How to fix this?

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • You said you set the listener in MainActivity, but this fragment is actually part of SettingsActivity, so I guess you should be setting the listener in SettingsActivity instead? – ehehhh Nov 21 '15 at 19:45
  • IDK what preference you modify in the fragment (iirc that's defined in the xml) but there are more than just the `DefaultSharedPreferences`. Maybe you're listening for changes on the wrong prefs? – zapl Nov 21 '15 at 19:47
  • @ehehhh: The second code segment is from the SettingsActivity. The first from the MainActivity. I want to know about changes in the MainActivity so I can customize it when the setting changes. – juergen d Nov 21 '15 at 19:47
  • @zapl: Yes I listen probably to the wrong one. But since I a new with Android development I have no idea how to listen to the SettingsPref or how to get the SettingsAcitivty to write to the AppPrefs. – juergen d Nov 21 '15 at 19:48
  • 1
    You're also not getting notified when you're setting the listener in a different activity than the one changing preferences. There is only 1 activity active and if you switch to the settings one and come back to your activity you'll simply have to check what the preferences are once you're restarted. Without listener. Listener is only needed for cases in which prefs change on you without you knowing about that. what fractalwrench says in long: http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently – zapl Nov 21 '15 at 20:27
  • @zapl: If you put that in an answer I will accept. – juergen d Nov 21 '15 at 23:07

1 Answers1

0

You need to hold onto a reference of your listener in your SettingsActivity. SharedPreferences only holds onto a Weak Reference, which means your listener doesn't get called.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49