Problem looks like that. In my app I have 3 activities (so far, fourth is comming). One of them is settings screen where user can choose his language. When settings are changed I update activities and now magic comes... all activities change their language except one - main menu activity. No matter what I do it works for all activities but this one. Funny thing is that I've checked and method to update locals is done and also proper language setting is used.
Main activity code (I've removed code that doesn't matter in this case):
public class MainMenuActivity extends AppCompatActivity {
private Preferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
initialize();
}
@Override
public void onResume() {
applySettings();
super.onResume();
}
private void initialize() {
preferences = Preferences.getInstance();
preferences.initiate(this);
}
private void applySettings() {
Toast.makeText(this, getResources().getConfiguration().locale.getDisplayName(), Toast.LENGTH_LONG).show();
Activity.setLocale(getApplicationContext(), preferences.getLanguage().getValue());
}
}
Here's method that sets locals:
public class ActivitySettings {
public static void setLocale(Context context, String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration();
conf.locale = locale;
Resources res = context.getResources();
res.updateConfiguration(conf, res.getDisplayMetrics());
}
As you see I've added line to show current language before update, and it show that language is correct. Why it won't change language for main activity?!
EDIT - found a solution
I've added new variable in my main activity
private Locale currentLocal = null;
Then on initiation I set this to current locale
currentLocal = Locale.getDefault();
Now in onResume() I've added that
if (currentLocal.getLanguage() != Locale.getDefault().getLanguage()) {
currentLocal = Locale.getDefault();
recreate();
}
Now it works perfect.