There is an application consisting of several activity. In this case, MainScreen and Setting. In Setting there is a choice of language, it works. After clicking on the button, the language changes, activity restarts and the entire application is in the language I need. I select the user in the settings.
The application will be turned off sooner or later. When turned on, MainScreen is launched, the settings file is read and the language is changed to the previously saved one. // So it should be in my view, and so it is implemented. However, when restarting, nothing changes and I can not understand why. Tell me please.
Here for Setting
Button ExitBtn; Button ChangeLang_Ru; Button ChangeLang_En; Button ChangeLang_De; // Инициализируем файл настроек public static final String APP_PREFERENCES = "mysettings"; public static final String APP_PREFERENCES_LANG = "LANG"; // Параметр языка SharedPreferences mSettings; // Отключено = 0 Integer audio_on = 0; Locale myLocale; String lang_set = "ru"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hideStatusBar(); setContentView(R.layout.setting); ExitBtn = (Button) findViewById(R.id.ExitBtn); ExitBtn.setOnClickListener(this); ChangeLang_Ru = (Button) findViewById(R.id.ChangeLang_Ru); ChangeLang_Ru.setOnClickListener(this); ChangeLang_En = (Button) findViewById(R.id.ChangeLang_En); ChangeLang_En.setOnClickListener(this); ChangeLang_De = (Button) findViewById(R.id.ChangeLang_De); ChangeLang_De.setOnClickListener(this); // Инициализируем настройки mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); } //Слушаем кнопки, выполняем переход @Override public void onClick(View v) { switch (v.getId()) { case R.id.ExitBtn: finish(); Intent menu_intent = new Intent(this, MainScreen.class); startActivity(menu_intent); break; case R.id.ChangeLang_Ru: SharedPreferences.Editor editor1 = mSettings.edit(); editor1.putString(APP_PREFERENCES_LANG, "ru"); editor1.apply(); setLocale("ru"); break; case R.id.ChangeLang_En: SharedPreferences.Editor editor2 = mSettings.edit(); editor2.putString(APP_PREFERENCES_LANG, "en"); editor2.apply(); setLocale("en"); break; case R.id.ChangeLang_De: SharedPreferences.Editor editor3 = mSettings.edit(); editor3.putString(APP_PREFERENCES_LANG, "de"); editor3.apply(); setLocale("de"); break; default: break; } } // Меняет язык public void setLocale(String lang) { myLocale = new Locale(lang); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); Intent refresh = new Intent(this, Setting.class); startActivity(refresh); } }
And accordingly for MainScreen
// Инициализируем файл настроек public static final String APP_PREFERENCES = "mysettings"; public static final String APP_PREFERENCES_LANG = "LANG"; // Параметр языка SharedPreferences mSettings; String lang_set = "ru"; Locale myLocale; @Override protected void onCreate(Bundle savedInstanceState) { // Инициализируем настройки mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); if(mSettings.contains(APP_PREFERENCES_LANG)) { lang_set = mSettings.getString(APP_PREFERENCES_LANG, ""); } switch(lang_set) { case "ru": setLocale("ru"); break; case "en": setLocale("en"); break; case "de": setLocale("de"); break; default: break; } super.onCreate(savedInstanceState); hideStatusBar(); setContentView(R.layout.activity_main_screen); } public void setLocale(String lang) { myLocale = new Locale(lang); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); //Эта часть здесь по всей видимости лишняя //Intent refresh = new Intent(this, MainScreen.class); //startActivity(refresh); } In the manifest for all the activity is written the following:
android:configChanges="locale"
mSettingsin both places. Perhaps these are 2 completely unrelatedSharedPreferences- Vladyslav Matviienkorecreate(). And your value from preferences is not used anywhere, although it should apply at the very beginning. - YurySPb ♦