'onOptionsItem is not localized in Android Studio
When I change the language of the app, everything translated to the selected language at the time, but after restarting the app, Option Items are translated back to the system language of the device:

I translated the app to Persian language(like Arabic), also this back button, goes back to the left side of the app bar, after app restart.

I extend all the activities from my custom AppCompat Activity:
public class AppCompat extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LanguageManager languageManager = new LanguageManager(this);
languageManager.updateResources(languageManager.getLang());
}
}
And This is Language Manager Class
public class LanguageManager {
private final Context context;
private final SharedPreferences sharedPreferences;
public LanguageManager(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences("LANG", Context.MODE_PRIVATE);
}
public void updateResources(String code) {
Locale locale = new Locale(code);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
setLanguage(code);
}
public void setLanguage(String code) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("lang", code);
editor.apply();
}
public String getLang() {
return sharedPreferences.getString("lang", "en");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
