user7097645
user7097645

Reputation:

language restored to it's default after restarting or rotating screen

I have an app that supports two languages, the app is working very well when i change the language, but when i restart the app or rotate the screen, language is restored to it's default.

What i'm trying to do is to save the language and then the app should work with last saved language even after restart or rotate the screen.

I did a lot of researches and found some solutions which talks about localeHelper and Application classes and other ways but anyway non of them helped me, or maybe i'm not understanding them well.

Thanks in advance.

here is my code:

@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;
}

}

Upvotes: 0

Views: 111

Answers (1)

user2711811
user2711811

Reputation:

In checkLanguage use compareTo (or equals) not reference equality (==) for all your string comparisons ('ar', 'en'...).

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

or

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

See https://stackoverflow.com/a/513839/2711811

Upvotes: 1

Related Questions