Android Preferences: How to load the default value

2019-01-05 08:30发布

I am using a PreferenceActivity to let the user set some values. I am feeding it the xml file with the defined preferences.

I have set all the android:defaultValue="" for them.

When I start my application, I need the preferences, or if they are not set yet manually, I want the default values:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean value = prefs.getBoolean("key"), false); 

However, when android:defaultValue="true" I still get false. So, it looks like the defaultValues set in the XML are not used anywhere but when initializing the preferences-screen.

I don't want to hardcode the default values in the getBoolean() method. So, is there a way get the default-values with only defining these in 1 place?

5条回答
别忘想泡老子
2楼-- · 2019-01-05 09:11

this question is similar to mine:

initialize-preferences-from-xml-in-main-activity

Just use this code in onCreate method:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

It will load your preferences from XML, and last parameter (readAgain) will guarantee that user preferences won't be overwritten.

Take a look into PreferenceManager.setDefaultValues in Android API for further investigation.

查看更多
淡お忘
3楼-- · 2019-01-05 09:19

Be aware that if you are using
getSharedPreferences(String sharedPreferencesName, int sharedPreferencesMode)

to retrieve preferences you have to use
PreferenceManager.setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
to set defaults!

For example:
PreferenceManager.setDefaultValues(this, PREFS_NAME, Context.MODE_PRIVATE, R.xml.preference, false);

I hope this can help someone.

查看更多
The star\"
4楼-- · 2019-01-05 09:20

in Pixel's accepted answer:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

it is stated that the false means that defaults won't be overwritten. This is not what it does, it is just an efficiency flag to stop the parsing if your application has more than one entry point. Unfortunately the test is not made per preference file, so if you have more than one preference file you must code true on all but the first.

If you are worried about efficiency, you could code something like this.

final static private int SPL = 1;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getInt("spl", 0) != SPL)
{
    PreferenceManager.setDefaultValues(this, R.xml.prefs1, true);
    PreferenceManager.setDefaultValues(this, R.xml.prefs2, true);
    sp.edit().putInt("spl", SPL).apply();
}

If you ever add more shared preferences, just set SPL to a hight number.

查看更多
相关推荐>>
5楼-- · 2019-01-05 09:26

Also make sure you have never used the SharedPreferences before. To make sure they are not changed (which means setDefaultValues(this,xml,false) has no effect) uninstall your App and upload it again to be sure no values are touched. This helped me.

查看更多
趁早两清
6楼-- · 2019-01-05 09:28

For example extending DialogPreference I do this:

@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
    super.onSetInitialValue(restore, defaultValue);

    if (restore) {
        mValue = shouldPersist() ? getPersistedString(mDefault) : mDefault;
    } else {
        mValue = mDefault;
    }
}

mDefault can be:

  • mContext.getResources().getString(attrs.getAttributeResourceValue(androidns,"defaultValue", 100));
  • something you have indexed in R.
查看更多
登录 后发表回答