77

I have a class that extends PreferenceActivity and shows the preference screen of my app. Is it possible to check if any changes were made to the preferences?

This helps...

http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html

Other related post: SharedPreferences.onSharedPreferenceChangeListener not being called consistently


public class PreferenceClass extends PreferenceActivity {

    OnSharedPreferenceChangeListener listener;

    public void onCreate(Bundle savedInstanceState) {
        SharedPreferences prefs = this.getSharedPreferences("settings", 0);
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {

            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                int flag = 1;
            }
        };
        prefs.registerOnSharedPreferenceChangeListener(listener);
        super.onCreate(null);
        addPreferencesFromResource(R.xml.settings);
    }
}
Community
  • 1
  • 1
mixkat
  • 3,883
  • 10
  • 40
  • 58
  • I see too many directions this could possibly go depending on your overall goal. Could you toss out some specifics about when you are checking and to what end? – R Hughes Feb 14 '11 at 22:48
  • @RHughes I just want everytime a change is made to do something...Just found this link http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently... Still cant get it to work though...the method in the listener never gets called...Any ideas??? – mixkat Feb 14 '11 at 23:14
  • Without looking at the code, I can only guess that you are making the same mistake I always seem to make. I build a great listener then forget to attach it to the right object. Also, make sure you are attaching the listener to the object in the onCreate() method or at least in code that you run BEFORE you actually need the listener to be listening. – R Hughes Feb 14 '11 at 23:38
  • @RHughes No Im attaching the listener to the object...donno whats wrong with it...its definitely on create...anyways thanks for the reply... – mixkat Feb 15 '11 at 00:05

2 Answers2

68

Do

SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
                           SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // your stuff here
    }
};

In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method.

That's what I do and I never have a problem.

Else it's your conditional checking in the listener that is at fault. Post the code.

EDIT:

From the code you posted, you should make prefs a class member variable so it has a global scope.

And do prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); instead of getSharedPreferences because you haven't created that file.

To create a file you need to use PreferenceManager. To get a PreferenceManager, use Activity.getPreferenceManager().

100rabh
  • 6,156
  • 5
  • 27
  • 41
techi.services
  • 8,473
  • 4
  • 39
  • 42
  • @sugarynugs Thats exactly what I am doing...in fact i copied the code from this other post... – mixkat Feb 15 '11 at 00:00
  • @sugarynugs listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // Implementation } }); prefs.registerOnSharedPreferenceChangeListener(listener) – mixkat Feb 15 '11 at 00:01
  • 1
    @sugarynugs listener is an instance variable of this class..just to avoid it being gc.. – mixkat Feb 15 '11 at 00:03
  • 1
    where is the variable declaration? Is it a `Global`? Plus wrong code posted... the bit inside the listener. – techi.services Feb 15 '11 at 00:04
  • That was it...getDefaultSharedPreferences fixed it....thanks a lot mate... @sugarynugs – mixkat Feb 15 '11 at 16:50
  • don't forget to UNREGISTER listener with .unregisterOnSharedPreferenceChangeListener !!! – babay Jan 28 '13 at 17:55
  • @babay: Just wondering if you know if there is any need to try to unregister the listener if it is implemented in the Application class? (Doing that would be very difficult - the Application class has no onStop() or onDestroy() methods.) – RenniePet Mar 15 '16 at 02:33
  • 1
    @RenniePet, No, you don't have to unregister preferences listener that is registered in Application class. For example, It you don't unregister a listener in Activity class, then garbage collector will not release memory allocated by Activity object after activity closed (preference manager has a reference to listener, listener has a reference to activity). But your don't have that case with Application class. – babay Mar 16 '16 at 17:12
67

In your PreferenceActivity class, implement the SharedPreferences.OnSharedPreferenceChangeListener interface. Add the required onSharedPreferenceChanged method to your class and register it in the onCreate.

See sample code here:

public class MyPreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fw_preferences); //deprecated
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // handle the preference change here
    }

}
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
Jonathan
  • 1,078
  • 11
  • 9
  • 1
    How would you refactor the deprecated line you have in your code? `addPreferencesFromResource(R.xml.fw_preferences); //deprecated` – mattecalcio Apr 22 '15 at 19:03
  • 1
    @mattecalcio I know I'm late but here is the new way to load in a settings xml: http://stackoverflow.com/a/13441715/1318194 –  May 10 '15 at 05:16
  • 2
    Is it a good practice to do unregisterOnSharedPreferenceChangeListener(this) in onDestroy() of MainActivity? – Andrii Kovalchuk Aug 29 '16 at 12:16
  • 1
    your answer awesome but also docs says: add register part in resume and add unregister part in pause for garbage colecter. thanks again https://developer.android.com/guide/topics/ui/settings.html#ReadingPrefs – mehmet May 10 '17 at 07:32
  • Together with implementing the interface `OnSharedPreferenceChangeListener` and register listener on `onResume` and unregister on `onPause` – MRodrigues Dec 19 '17 at 10:15
  • Don't forget to call PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); on onDestroy or implement like @MRodrigues put above – Cícero Moura Jan 27 '20 at 21:33