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" 
  • show how you initialize mSettings in both places. Perhaps these are 2 completely unrelated SharedPreferences - Vladyslav Matviienko
  • @metalurgus updated description. - St. Ivan
  • The following clarifying question: You have not laid out the constants APP_PREFERENCES and APP_PREFERENCES_LANG. Are you 100% sure that their values ​​in different Activities are the same? - Vladyslav Matviienko 8:33
  • It is not clear why you run the same activation after changing the language, but before overwriting the value into preferences, instead of first updating the preference and then simply recreating the activation through recreate() . And your value from preferences is not used anywhere, although it should apply at the very beginning. - YurySPb
  • @metalurgus Yes, I thought that it was also necessary, and added constants. - St. Ivan

1 answer 1

You change the language in MainScreen after everything has already been displayed on the default. Call setLocale before setContentView and probably better before super.onCreate(savedInstanceState)

  • moved the whole construction of reading the settings and changing the language to super.onCreate(savedInstanceState) . Unfortunately, nothing has changed :( - St. Ivan
  • Tell me please, maybe I misunderstood you? What is the most offensive is to go to the settings and the language immediately such as needed, but before that in any way. - St. Ivan
  • hmm, well 100% option is to move the language setting to the application class. If you don’t have one yet, then create a class inherited from Application and set its language in onCreate as you did in the activation. Well, do not forget to register your new class in the manifest in the application tag in the attribute android:name - xkor
  • already figured out. Not exactly figured out to the end, but everything works. Thank you :) - St. Ivan