3

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.

huong
  • 4,534
  • 6
  • 33
  • 54
  • Listen **onResume** method in ActivityDestination or **onBackPressed** in ActivitySource. –  Jul 22 '13 at 09:26
  • if your using finish() to get back to the previous activity try to use intent instead so it will refresh the previous activity with the new language – mmoghrabi Jul 22 '13 at 09:31

4 Answers4

4

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!

huong
  • 4,534
  • 6
  • 33
  • 54
0

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.

Scott Helme
  • 4,786
  • 2
  • 23
  • 35
0

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.

Traxex1909
  • 2,650
  • 4
  • 20
  • 25
  • I assume you mean refresh the activity in `onResume`? Well I tried that and the activity can hardly start due to the infinite loops of finishing and starting. – huong Jul 22 '13 at 09:44
  • Unless you have any saved variables you can use `onCreate(null);`. That should restart your activity as well. – Traxex1909 Jul 22 '13 at 10:10
0

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

R W
  • 11
  • 2