Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to refresh the view of a Fragment

Tags:

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

like image 861
Leem.fin Avatar asked Mar 27 '12 12:03

Leem.fin


People also ask

How do you refresh a fragment on a resume?

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 .

How do I refresh a fragment from another fragment?

So when you click your Button , find the Fragment you want to refresh by its tag, and then call your refresh method.

Can a fragment have its own view?

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.

How do you refresh a fragment after onBackPressed from activity?

You can override onBackPressed in Activity and call a function on corresponding fragment to reloadItems().


1 Answers

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

like image 68
ZeeShaN AbbAs Avatar answered Sep 21 '22 04:09

ZeeShaN AbbAs