Reputation: 21
I want to change a preference when I click on another preference. I did this with onSharedPreferenceChanged
method and setting the value with editor like this...
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
Preference pref = findPreference(key);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("sound","2");
editor.commit();
Intent intent3 = new Intent(this, Prefer.class);
startActivity(intent3);
this.setSummary(pref);
However, I am getting an error at editor.commit();
and my code runs in the background for several times before giving StatckOverflow error... What am I doing wrong?
Than You
Upvotes: 2
Views: 575
Reputation: 76536
Your calling ,
onSharedPreferenceChanged
then your calling
editor.commit();
By commit() you are changing your shared preferences and therefore calling
onSharedPreferenceChanged
and so the cycle continues ..... till you StackOverflow
*My Christmas SharedPreferences API Link *
Upvotes: 7