当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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.

网友答案:

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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: