如何在更改语言后刷新活动?

时间:2017-09-10 15:02:12

标签: android localization

我搜索Google& stackoverflow但我无法解决我的问题。我引用this我有三项活动(活动1,2,3)。活动1>活动2>活动3(milti lang改变)。在活动3中,我有两个单选按钮要更改(en,hi)。我改变语言后;去第一个活动(活动1),没关系。但是,在选择单选按钮(更改语言)后,我想保持当前活动(刷新当前活动)。否则,当我回到onBackPressed()时,如何调用onCreate()。如何在改变语言后保持当前活动的最佳方式。

的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    txt = (MMTextView) findViewById(R.id.txt);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

    pref = getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    editor = pref.edit();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            String lang = "en";
            switch (i) {
                case R.id.eng:
                    lang = "en";
                    Intent i1 = new Intent(Main3Activity.this, MainActivity.class);
                    i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i1);
                    finish();
                    break;
                case R.id.hi:
                    lang = "hi";
                    Intent i2 = new Intent(Main3Activity.this, MainActivity.class);
                    i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i2);
                    finish();
                    break;
                default:
                    break;
            }
            changeLocale(lang);
        }
    });

    loadLocale();
}

changeLocale

private void changeLocale(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;
    Locale myLocale = new Locale(lang); 
    saveLocale(lang);  
    Locale.setDefault(myLocale);    
    Configuration config = new Configuration(); 
    config.locale = myLocale;   
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());  
}

saveLocale& loadLocale

private void saveLocale(String lang) {
    editor.putString("save", lang);
    editor.commit();
}

private void loadLocale() {
    String lang = pref.getString("save", "");
    changeLocale(lang);
}

4 个答案:

答案 0 :(得分:0)

您可以关闭并打开相同的活动,例如:

startActivity(new Intent(ActivityName.this, ActivityName.class));
finish();

我希望这有效。

答案 1 :(得分:0)

/***You need to create separate String file for both arabic and 
English***/

English
Values
String.xml
Hindi   
Values-hi
String.xml

/**You can change the application language using the following code:**/

language =  "en"; or  language =  "hi";

@Override
public void recreate()
{
if (android.os.Build.VERSION.SDK_INT >= 14)
{
    super.recreate();
}
else
{
    startActivity(getIntent());
    finish();
}
}

private void updateLanguage(String language)
{
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",language);
editor.apply();

recreate();
}

答案 2 :(得分:0)

使用recreate()重新启动当前活动。

答案 3 :(得分:0)

当用户选择不同的语言时调用下面的函数

 public void setApplicationLanguage(String language) {
    Locale myLocale = new Locale(language);
    Resources res = getBaseContext().getResources();
    DisplayMetrics display = res.getDisplayMetrics();
    Configuration configuration = res.getConfiguration();
    configuration.locale = myLocale;
    res.updateConfiguration(configuration, display);
    Intent refresh = new Intent(getBaseContext(), ChooserActivity.class);
    getBaseContext().startActivity(refresh);
}