I changed the locale
of my application programmatically, like following:
Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale("cn"); res.updateConfiguration(conf, dm);
After this, I would like to refresh my currently displayed fragment's view to display with new locale settings.
So, I firstly get the current fragment (that's the latest one in backstack
):
BackStackEntry latestEntry=fragMgr.getBackStackEntryAt(fragMgr.getBackStackEntryCount()-1); String str=latestEntry.getName(); Fragment fragment=fragMgr.findFragmentByTag(str);
After I get the currently showing fragment, I would like to refresh its view, but how to do it? Is it possible in Android to refresh the view of a fragment
The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume .
So when you click your Button , find the Fragment you want to refresh by its tag, and then call your refresh method.
Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities. Fragments are standalone components that can contain views, events and logic.
You can override onBackPressed in Activity and call a function on corresponding fragment to reloadItems().
You can simply detach and attach fragment as below
Fragment currentFragment = getFragmentManager().findFragmentByTag("FRAGMENT"); FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.detach(currentFragment); fragTransaction.attach(currentFragment); fragTransaction.commit();
This will refresh the view and locale will change
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With