How to update a fragment after changing the language applied in android?

Fragments are widely used in my application. It is supposed to support 3 languages ​​- English, Arabic and Kurdish. The main screen consists of a navigation box, in which there is a menu of options, one of which is the languages ​​that open the language selection dialog. The language selector has 3 buttons - English, Kurdish and Arabic, pressing the button changes the language like this:

private void setLocale(String code){ Configuration config=new Configuration(); Locale locale = null; locale=new Locale(code); Locale.setDefault(locale); config.locale=locale; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); dismiss(); } 

I added the following code in onConfigurationChange -

 FragmentManager fragmentManager = getSupportFragmentManager(); int count=fragmentManager.getBackStackEntryCount(); BackStackEntry latestEntry=(BackStackEntry) fragmentManager.getBackStackEntryAt(count-1); String str=latestEntry.getName(); Fragment fragment=fragmentManager.findFragmentByTag(str); if(fragment==null){ return; } FragmentTransaction fragTransaction = fragmentManager.beginTransaction(); fragTransaction.detach(fragment); fragTransaction.attach(fragment); fragTransaction.commit(); 

And I added in android manifest - the following:

 android:configChanges="locale" 

but nothing similar happens. I also tried - Is it possible to update a fragment view without much luck. Any help is appreciated. Thanks in advance.

+5
source share
1 answer
It seems to me that if you are listening to configuration changes, you should also override the onConfigurationChanged method either in the Fragment or in the hosting activity and then update the user interface using the new locale, as necessary, instead of detaching and reconnecting, you simply apply method to the children of the activity and re-fill all the fields as necessary.
0
source

Source: https://habr.com/ru/post/1213705/


All Articles