1

I have

SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

and

myPreferences.getBoolean("checkbox", true)

So, how can my main activity know when user change preferences state? For example if user don't wont to get notifications any more. Thanks.

Wolf87
  • 540
  • 3
  • 11
  • 29

3 Answers3

1

You could either re-read all the preferences in Activity.onResume(), or as long as your main activity launches the preferences activity (e.g. from a menu), you could launch with startActivityForResult(Intent, int) and re-read all the preferences in onActivityResult(int, int, Intent).

I have also found it sufficient to re-check each relevant preference before starting the appropriate behavior (i.e. before issuing the Notification for your example).

Rob I
  • 5,627
  • 2
  • 21
  • 28
1

You need to implement the onSharedPreferenceChangeListener interface. Then register to receive change updates in your onCreate/onStart:

myPreferences.registerOnSharedPreferenceChangeListener();

In your listener you do something like this:

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals("MyKey")) {
    // Do something.
} else if (key.equals("MyOtherKey")) {
    // Do something else.
      }
}

Remember to remove the listener in onStop/onDestroy.

jsmith
  • 4,847
  • 2
  • 32
  • 39
  • I tried registerOnSharedPreferenceChangeListener() but it doesn't work. By default preference checkbox is set to true, so when user change it I want to stop service working? – Wolf87 May 16 '12 at 13:52
  • Where do you set the default value? I use 'PreferenceManager.setDefaultValues(this, R.xml.preferences, false)'. The setDefaultValues and the registerOnSharedPreferenceChangeListener all happen within my service. Are you sure your servce is working at the time of the change? – jsmith May 16 '12 at 14:12
  • I found solution in [here](http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently/3104265#3104265) – Wolf87 May 16 '12 at 14:32
  • That's also good to know. Mental note: don't unse annonymous inner class for preference callbacks. – jsmith May 16 '12 at 16:26
0

You can create a static boolean that is changed whenever a preference is changed, or when your preference activity is started. The purpose of this boolean is to let your main activity know the preferences are dirty and need to be reloaded. They should be reloaded onResume if the mDirty flag is true.

Another option is to just reload all the preferences onResume, regardless. This may not be as efficient, but if you don't have loads of preferences, it's fine.

The most efficient way would be to set onPreferenceChanged listeners for all your prefs in your prefs activity, the prefs then notify the activity only when they actually change. This solves the case when your user enters your prefs activity, but doesn't actually change anything.

dcow
  • 7,765
  • 3
  • 45
  • 65