Search code examples
androidonresume

Changing locale not working after `onResume` method?


I have an app that is (more or less) translated in other languages. For this I used locale and for saving the language preferred by the user I used SharedPreferences.

Using this I managed to load the prefered language on any/every activity inside the app.

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

    findViewById(R.id.flagcollection).setVisibility(View.GONE);

    findViewById(R.id.worldmapswitch)
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (flagWorld) {
                        // means true
                        findViewById(R.id.flagcollection).setVisibility(View.GONE);
                        flagWorld = false;
                    } else {
                        findViewById(R.id.flagcollection).setVisibility(View.VISIBLE);
                        flagWorld = true;
                    }
                }
            });

    preferences = getSharedPreferences("settings_values", MODE_PRIVATE);

    String SavedLocaleLang = preferences.getString("LocalLang", null);
    Locale locale = new Locale(SavedLocaleLang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
....

The problem is that if for whatever reason I leave the app (not exiting/ending activity), after a while when I return to the app, it runs using the PHONE'S default language and not the saved value for locale.

Then, after reading numerous posts regarding the miracles of onResume, combined with my java no-skills I tried this :

@Override
public void onResume(){
    super.onResume();
    // put your code here...

    preferences = getSharedPreferences("settings_values", MODE_PRIVATE);

    String SavedLocaleLang = preferences.getString("LocalLang", null);
    Locale locale = new Locale(SavedLocaleLang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());


}

I thought that I have solved my problem, but after testing the result in less than 1 hour I realized that I've done nothing (at least nothing good). Somewhere I'm doing something wrong.

If anyone has the skills, the patience and much, much good will, PLEASE help me understand what and where I am doing wrong.

Any help will be greatly appreciated! No sarcasm though, please. There I'm doing just fine :P

THANK YOU !!!

P.S. User "Cochi" requested the code for saving the locale in Prefereces ... so here you go :

findViewById(R.id.flaghungary)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String localeHU = "hu";
                    SharedPreferences settings = getSharedPreferences("settings_values", MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("LocalLang", localeHU);
                    editor.apply();

                    Locale locale = new Locale("hu");
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
                    setContentView(R.layout.activity_pick);
                    Context context = getApplicationContext();
                    Toast.makeText(context, "Magyar nyelv kiválasztva", Toast.LENGTH_SHORT).show();
                    recreate();
                }


            });

Solution

  • Try putting that code inside onCreate, before the setContentView :

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        String SavedLocaleLang = preferences.getString("LocalLang", null);
        Locale locale = new Locale(SavedLocaleLang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }