andreasperelli
andreasperelli

Reputation: 1034

boolean SharedPreferences always loading default values

I've a strange issue with SharedPreferences and boolean. I've set this code in my xml:

xml:

 <CheckBoxPreference
             android:key="onlywifiupload"
             android:defaultValue="true"
             android:summary="@string/summary_onlywifiupload"
             android:title="@string/title_onlywifiupload" 
      />

and from the Java code, I'm calling:

 boolean onlywifiupload =  pref.getBoolean("onlywifiupload", true);

Even the checkbox is checked or unchecked, in onlywifiupload there's always true. Same with setting:

 boolean onlywifiupload =  pref.getBoolean("onlywifiupload", false);

It seems the default value is always loaded instead of checked values.

Upvotes: 1

Views: 1015

Answers (1)

andreasperelli
andreasperelli

Reputation: 1034

It seems the only way to make it working is:

    mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    onlywifiupload = mPrefs.getBoolean("onlywifiupload", true);

don't know why I need to call getDefaultSharedPreferences from PrefenceManager object

Before that, I was calling the preferences in this way:

    pref = getSharedPreferences("AppPref", MODE_PRIVATE);

Upvotes: 1

Related Questions