Question

I have this settings section where I allow users to change the languages displayed within the app. When the user chooses a different language, the activity is reloaded so that the change of language can be applied. But the problem is, when the user clicks back right after changing the language, the language shown in the background activity is still the same. So my question is, what should I do to apply the change of language when I get back to some activity on the background? I suppose I should do something to detect the change in onResume method, but I'm not sure what it is. If you have any suggestions, please let me know. Thank you.

Was it helpful?

Solution

After several attempts, I have found the solution to my problem. On my onCreate method, I get the SharedPreferences that contains the value of current language, and get the current language:

SharedPrefrences languagepref = getSharedPreferences("language",MODE_PRIVATE);
String language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());

Then, in my onResume method, I assign the value of the above mentioned variable language to a local variable, and update the value of language. Then I compare these two variables - if they are different, I will destroy the current activity and start another:

@Override
    public void onResume(){
        super.onResume();       

        String oldLanguage = language;
        language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
        if (!oldLanguage.equals(language)){
            finish();
            startActivity(getIntent());
        }
    }

And voila, that did the trick!

OTHER TIPS

I would suggest using SharedPreferences. You can store a lang key with the associated value in there and update it when necessary. In your onResume() methods you can get the lang value and then populate views according the value stored.

SharedPreferences sharedPreferences; 
sharedPreferences = this.getSharedPreferences("MyActivity", Activity.MODE_PRIVATE); 
String lang = sharedPreferences.getString("lang", "en-GB");

SharedPreferences.Editor editor; 
editor = sharedPreferences.edit(); 
editor.putString("lang", "en-US").commit(); 

That's the basics you need to get going.

Have you tried restarting the Activity after the change is done ?

You can Simply use

finish();
startActivity(getIntent());

to refresh an activity whenever it detects a preference change.

The built in back pressed function does not refresh the code,so do this after u you change the language.

@Override
public void onBackPressed()
{
    //new MainActivity();
    Intent intent=new Intent(this,MainActivity.class);
    startActivity(intent);
}

and in Main Activity class do this

  public MainActivity() {
    //1- This code is responsible for updating the change of all Strings from a language to another
    //it will be called every time this activity is instantiated (da,since it is a constructor) , or when this activity gets
    // called by an intent.
    //2- Every String inside this Activity(ever fragment inside it ) will also be under the effect of change of language
    LocalUtils.updateConfig(this);
}

Please review the following answer and add this to it. To get a full answer written by Roberto.B and LarsH: Changing Locale within the app itself

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top