Okay I started to implement this horrible code from Google’s android. The OnSharedPreferenceChangeListener is not being called. This is my code, can you please advice?
Class definition:
private SharedPreferences sPrefs;
private PreferenceChangedListener prefsChangedListener;
I have a private inner class:
private class PreferenceChangedListener implements
OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
if (key == "highThreshold") {
try {
highThreshold = Float.parseFloat(sharedPreferences
.getString(key, "0"));
} catch (Exception e) {
}
}
}
}
Tried te following code in OnResume and to register the listener after the ‘Voorkeuren’ preferenceactivity is started. Both fail.
sPrefs = getPreferences(MODE_PRIVATE);
prefsChangedListener = new PreferenceChangedListener();
sPrefs.registerOnSharedPreferenceChangeListener(prefsChangedListener );
I defined a class Voorkeuren which extends PreferenceActivity
public class Voorkeuren extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_voorkeuren, menu);
return true;
}
}
Which I open as following:
Intent intent = new Intent(getBaseContext(),Voorkeuren.class);
startActivity(intent);
The class shows up fine, and stores the values between sessions. But my application should not be polling if settings chagned. Any ideas? I have red something about an SharedPreferences.Editor but I am not sure how it is related.
Not sure what your question is and what is desired.
But there is one change that you need to do. Replace this in onResume
to
in onCreate()