2

When I change the locale/language of my phone, my parameters (selected theme and target language for translation function) which are saved on sharedPreference turn to default values. For example, default values of preferences are "light" for the theme and "en" for the target language. I change the preferences as "dark" for the theme and "de" for the target language. If I change the locale from the Settings, theme preference turns to "light" and target language preference turns to "en" again. How can I prevent this change?

I have already tried onConfigurationChanged() method, but it is only triggered while the activity is running. It is not triggered when the locale is changed while the activity is not running.

Here is the onPreferenceChange(Preference preference, Object value) method:

        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 Answers1

1

I have solved the problem. The reason for the error was using localized keys for preferences. Since keys were changing for each language, sharedPreferences were updated. I kept the preference keys same for each locale and the problem is solved. I hope it can be a solution if someone experiences the same problem.