How to detect if changes were made in the preferences?
Solution 1
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()
.
Solution 2
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
}
}
Related videos on Youtube
mixkat
Updated on July 05, 2022Comments
-
mixkat almost 2 years
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...
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); } }
-
R Hughes over 13 yearsI 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?
-
mixkat over 13 years@RHughes I just want everytime a change is made to do something...Just found this link stackoverflow.com/questions/2542938/…... Still cant get it to work though...the method in the listener never gets called...Any ideas???
-
R Hughes over 13 yearsWithout 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.
-
mixkat over 13 years@RHughes No Im attaching the listener to the object...donno whats wrong with it...its definitely on create...anyways thanks for the reply...
-
-
mixkat over 13 years@sugarynugs Thats exactly what I am doing...in fact i copied the code from this other post...
-
mixkat over 13 years@sugarynugs listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // Implementation } }); prefs.registerOnSharedPreferenceChangeListener(listener)
-
mixkat over 13 years@sugarynugs listener is an instance variable of this class..just to avoid it being gc..
-
techi.services over 13 yearswhere is the variable declaration? Is it a
Global
? Plus wrong code posted... the bit inside the listener. -
mixkat over 13 yearsThat was it...getDefaultSharedPreferences fixed it....thanks a lot mate... @sugarynugs
-
babay over 11 yearsdon't forget to UNREGISTER listener with .unregisterOnSharedPreferenceChangeListener !!!
-
mattecalcio about 9 yearsHow would you refactor the deprecated line you have in your code?
addPreferencesFromResource(R.xml.fw_preferences); //deprecated
-
Admin about 9 years@mattecalcio I know I'm late but here is the new way to load in a settings xml: stackoverflow.com/a/13441715/1318194
-
RenniePet over 8 years@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.)
-
babay over 8 years@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.
-
Andrii Kovalchuk almost 8 yearsIs it a good practice to do unregisterOnSharedPreferenceChangeListener(this) in onDestroy() of MainActivity?
-
mehmet about 7 yearsyour answer awesome but also docs says: add register part in resume and add unregister part in pause for garbage colecter. thanks again developer.android.com/guide/topics/ui/…
-
MRodrigues over 6 yearsTogether with implementing the interface
OnSharedPreferenceChangeListener
and register listener ononResume
and unregister ononPause
-
Cícero Moura over 4 yearsDon't forget to call PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); on onDestroy or implement like @MRodrigues put above