user1102086
user1102086

Reputation: 21

Android SharedPreferences not getting changed through editor.commit

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

Answers (1)

Blundell
Blundell

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

Related Questions