Android Preferences: How to load the default values when the user hasn't used the preferences-screen?

后端 未结 6 1182
春和景丽
春和景丽 2020-11-28 02:02

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:defaultV

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 02:18

    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.

提交回复
热议问题