Reputation: 918
My app uses fragments extensively. It is supposed to support 3 languages - English, Arabic and Kurdish. The home screen consists of a navigation drawer which holds a menu of options one of them is languages which opens a language selector dialog. The language selector has 3 buttons - English, Kurdish and Arabic clicking on a button changes the language like so -
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 have added the following code to 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 have added the following to android manifest -
android:configChanges="locale"
but nothing seems to be happening. I also tried - Is it possible to refresh the view of a Fragment without much luck. Any help is appreciated. Thanks in advance.
Upvotes: 9
Views: 7309
Reputation: 12587
seems to me that if you listen to configuration changes you should also override the onConfigurationChanged
method in either the Fragment or the hosting activity and then update the UI using the new Locale as needed instead of detaching and re-attaching you just popegate a method into the children of the activity and re-populate all the fields as needed
Upvotes: 0