重新启动或旋转屏幕后,语言恢复为默认语言

时间:2019-04-15 16:15:11

标签: java android

我有一个支持两种语言的应用程序,当我更改语言时,该应用程序运行良好,但是当我重新启动该应用程序或旋转屏幕时,该语言将恢复为默认设置。

我要执行的操作是保存语言,然后即使重新启动或旋转屏幕,应用也应使用上次保存的语言。

我做了很多研究,发现了一些解决方案,这些解决方案讨论了localeHelper和Application类以及其他方法,但是无论如何都没有帮助我,或者我不太了解它们。

谢谢。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkLanguage();

    setContentView(R.layout.activity_profile);

    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Intent i;
            switch(menuItem.getItemId()){
                case R.id.reset_app:
                    startActivity(new Intent(ProfileActivity.this, MainActivity.class));
                    break;
                case R.id.arabic:
                    PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("language", "ar").commit();
                    language("ar");
                    i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    break;
                case R.id.english:
                    PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("language", "en").commit();
                    language("en");
                    i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    break;
            }
            return true;
        }
    });

    Boolean isFirstRun = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("isFirstRun", true);
    if (isFirstRun) {
        //show MainActivity
        startActivity(new Intent(ProfileActivity.this, MainActivity.class));
    }
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("isFirstRun", false).commit();

    nameTextView = findViewById(R.id.name);
    nameTextView.setText(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("name", ""));
    mobileTextVew = findViewById(R.id.mobile_number);
    mobileTextVew.setText(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("mobile", ""));
    idTextView = findViewById(R.id.id);
    idTextView.setText(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("id", ""));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

public void language(String langCode){
    Resources res = getResources();
    String languageToLoad  = langCode;
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    res.updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

public void checkLanguage(){
    String langCode = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("language",null );
    if(langCode == "ar")
        language(langCode);
    else if(langCode == "en")
        language(langCode);
    else
        return;
}

}

1 个答案:

答案 0 :(得分:0)

checkLanguage中,对于所有字符串比较('ar','en'...),请使用compareTo(或equals)而不是引用等于(==

if (langCode.compareTo("ar") == 0) {
   ...
}

if (langCode.equals("ar")) {
}

请参见https://stackoverflow.com/a/513839/2711811