简体   繁体   English

以编程方式将共享首选项设置为默认值

[英]set shared preference default programmatically

I have a shared preference with no default value defined in the xml because I would like to set the default value programmatically when the main activity is created. 我有一个共享首选项,没有在xml中定义默认值,因为我想在创建主要活动时以编程方式设置默认值。 The preference in question should either be the device language (if that language is available), otherwise the first available language, where the languages are defined in the appropriate xml as a string array. 有问题的首选项应该是设备语言(如果可以使用该语言),否则应该是第一种可用的语言,其中在相应的xml中将这些语言定义为字符串数组。

I currently have, inside the main activity's onCreate method, the following 我目前在主要活动的onCreate方法中具有以下内容

    SharedPreferences preferences = getSharedPreferences(
            getString(R.string.shared_prefs_key),
            MODE_PRIVATE
    );

    String detailLanguage = preferences.getString(
            getResources().getString(R.string.detail_display_language),
            ""
    );

    if (detailLanguage.isEmpty()) {
        String deviceLanguage = Locale.getDefault().getLanguage();

        String[] availableLanguages = getResources().getStringArray(R.array.pref_entry_values_detail_display_language);
        boolean deviceLanguageSupported = false;
        for (String availableLanguage : availableLanguages) {
            if (deviceLanguage.equals(availableLanguage)) {
                deviceLanguageSupported = true;
                break;
            }
        }

        preferences.edit()
                .putString(
                        getResources()
                                .getString(R.string.detail_display_language),
                        deviceLanguageSupported ? deviceLanguage : availableLanguages[0]
                ).commit();

Debugging this code on the first run it enters the above if statement and sets the preference but the preference doesn't show as selected in the preference activity first time around (if I then select it myself, from then onward it appears selected as something). 第一次运行时调试该代码,它会进入上述if语句并设置首选项,但是首选项在第一次活动中未显示为首选项(如果我本人随后选择了它,从那时起,它就好像被选择为某物) 。

Am I missing something? 我想念什么吗? I am fairly new to Android. 我刚接触Android。 I tried the above based on some other similar SO questions. 我根据其他一些类似的SO问题尝试了上述方法。

PreferenceActivity and PreferenceFragment read preferences from a default preference file. PreferenceActivityPreferenceFragment从默认首选项文件中读取首选项。 To get access to those preferences you should use PreferenceManager.getDefaultSharedPreferences(context) method. 要访问这些首选项,您应该使用PreferenceManager.getDefaultSharedPreferences(context)方法。

I am guessing that this is the reason you aren't seeing your changes: you write them to getString(R.string.shared_prefs_key) rather than to the default one. 我猜这是您看不到更改的原因:您将它们写入getString(R.string.shared_prefs_key)而不是默认值。

Try changing your first line to this: 尝试将第一行更改为此:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM