更改Android语言环境语言时保留当前的sharedPreferences

时间:2019-06-08 22:36:43

标签: android localization sharedpreferences android-configchanges

当我更改手机的语言环境/语言时,我保存在sharedPreference上的参数(为翻译功能选择的主题和目标语言)变为默认值。例如,首选项的默认值为主题的“浅”和目标语言的“ en”。我将主题的首选项更改为“暗”,将目标语言的首选项更改为“ de”。如果我从“设置”更改语言环境,则主题首选项将变为“浅色”,目标语言首选项将再次变为“ en”。如何防止这种变化?

我已经尝试过onConfigurationChanged()方法,但是它仅在活动运行时触发。在活动未运行时更改语言环境时不会触发该事件。

这是onPreferenceChange(首选项首选项,对象值)方法:

        String stringValueLanguage = value.toString();
        preference.setSummary(stringValueLanguage);

        switch (stringValueLanguage) {

            //Language selection preference is set according to different locales:
            case LanguagesEN.Constants.ENGLISH_VALUE:
            case LanguagesTR.Constants.ENGLISH_VALUE:
            case LanguagesDE.Constants.ENGLISH_VALUE:
            case LanguagesES.Constants.ENGLISH_VALUE:
            case LanguagesFR.Constants.ENGLISH_VALUE:
                language = LanguageCodes.Constants.ENGLISH_CODE_VALUE;
                break;

            case LanguagesEN.Constants.TURKISH_VALUE:
            case LanguagesTR.Constants.TURKISH_VALUE:
            case LanguagesDE.Constants.TURKISH_VALUE:
            case LanguagesES.Constants.TURKISH_VALUE:
            case LanguagesFR.Constants.TURKISH_VALUE:
                language = LanguageCodes.Constants.TURKISH_CODE_VALUE;
                break;

            case (LanguagesEN.Constants.GERMAN_VALUE):
            case (LanguagesTR.Constants.GERMAN_VALUE):
            case (LanguagesDE.Constants.GERMAN_VALUE):
            case (LanguagesES.Constants.GERMAN_VALUE):
            case (LanguagesFR.Constants.GERMAN_VALUE):
                language = LanguageCodes.Constants.GERMAN_CODE_VALUE;
                break;

            case (LanguagesEN.Constants.FRENCH_VALUE):
            case (LanguagesTR.Constants.FRENCH_VALUE):
            case (LanguagesDE.Constants.FRENCH_VALUE):
            case (LanguagesES.Constants.FRENCH_VALUE):
            case (LanguagesFR.Constants.FRENCH_VALUE):
                language = LanguageCodes.Constants.FRENCH_CODE_VALUE;
                break;

            case (LanguagesEN.Constants.SPANISH_VALUE):
            case (LanguagesTR.Constants.SPANISH_VALUE):
            case (LanguagesDE.Constants.SPANISH_VALUE):
            case (LanguagesES.Constants.SPANISH_VALUE):
            case (LanguagesFR.Constants.SPANISH_VALUE):
                language = LanguageCodes.Constants.SPANISH_CODE_VALUE;
                break;

        }

        //User's language preference is saved in order to be used in TranslateActivity:
        SharedPreferences myLanguagePrefs = getActivity().getSharedPreferences(languageKey, MODE_PRIVATE);
        SharedPreferences.Editor prefsEditorLanguage = myLanguagePrefs.edit();
        prefsEditorLanguage.putString(languageIdKey, language);
        prefsEditorLanguage.commit();


        //THEME_KEY
        String stringValueTheme = value.toString();
        preference.setSummary(stringValueTheme);

        //User's theme preference is saved in order to used in other Activities:
        SharedPreferences myThemePrefs = getActivity().getSharedPreferences(themeKey, MODE_PRIVATE);
        SharedPreferences.Editor prefsEditorTheme = myThemePrefs.edit();

        switch (stringValueTheme) {

            case (Themes.Constants.LIGHT):

                theme = Themes.Constants.LIGHT;
                prefsEditorTheme.putString(themeIdKey, theme);
                prefsEditorTheme.commit();

                restartSettingsActivityAfterThemeSelection();

                break;

            case (Themes.Constants.DARK):

                theme = Themes.Constants.DARK;
                prefsEditorTheme.putString(themeIdKey, theme);
                prefsEditorTheme.commit();

                restartSettingsActivityAfterThemeSelection();
                break;

        }

        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。错误的原因是使用本地化键作为首选项。由于每种语言的键都在变化,因此sharedPreferences已更新。我对每个语言环境都保留了相同的首选项键,问题得到了解决。我希望如果有人遇到同样的问题,这可以是一个解决方案。