Whenever I try to add this line it crashes my app. Am I not putting it in the right spot?
preferences.registerOnSharedPreferenceChangeListener(myActivity.this);
Here is my class
class Simulation extends View {
// I declare my program variables here
public Simulation(Context context) {
super(context);
// get the preferences
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
preferences
.registerOnSharedPreferenceChangeListener(myActivity.this);
String storedPreference = preferences.getString("nPref", "0");
}
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Log.i(TAG, "preferences changed!");
}
}
Thanks!
One note about the Answer, the prefListener needs to be a class field, not a local variable or it may get garbage collected.
SharedPreferences.onSharedPreferenceChangeListener not being called consistently
Do like this
and