Refresh or redraw Fragment's View
Since there doesn't appear to be a way to force the Fragment's view to be refreshed I ended up just detaching and re-attaching the fragment so the onCreateView()
is called again. This seems to work just how I wanted it to. I used this solution as a starting point but needed to add commitAllowingStateLoss()
to prevent a crash: https://stackoverflow.com/a/19409266/1234752. Allowing state loss is fine here because the Fragment is just reused and the data is still contained in it.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
myFragment.updateData(data.getString(NEW_BUTTON_TEXT));
// Need to use commitAllowingStateLoss() to prevent an IllegalStateException
// from being thrown
getSupportFragmentManager()
.beginTransaction()
.detach(mContactDetailsFragment)
.attach(mContactDetailsFragment)
.commitAllowingStateLoss();
}
}
Method to refresh Fragment content when data changed ( like recall onCreateView)
Detach and attach it with
Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();
or search fragment with
Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.container);
Refresh Fragment at reload
I think you want to refresh the fragment contents upon db update
If so, detach the fragment and reattach it
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG is the name you gave your fragment when you created it
This code is for support library.
If you're not supporting older devices, just use getFragmentManager instead of getSupportFragmentManager
[EDIT]
This method requires the Fragment to have a tag.
In case you don't have it, then @Hammer's method is what you need.
refresh the fragment when getting back from an activity or pressing back
I fixed the issue by replacing onStop() function to onPause() since the activity is not getting destroyed and it no longer loop the createRecentlyViewedButton() function hope this help somebody
here are the changes I made though
override fun onPause() {
super.onPause()
shouldRefreshOnResume = true
}
and
override fun onResume() {
super.onResume()
//shoudRefreshOnResume is a global var
if (shouldRefreshOnResume) {
val recentlyViewed = activity?.findViewById<LinearLayout>(R.id.recently_viewedView)
createRecentlyViewedButton(recentlyViewed!!)
}
}
Refresh fragment when change between tabs
When a Fragment
is made visible (i.e., the selected page in your ViewPager
), its setUserVisibleHint() method is called. You can override that method in your TwoFragment
and use it to trigger a refresh.
public class TwoFragment extends Fragment {
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Refresh your fragment here
}
}
Related Topics
How to Make Layouts for Several Android Screen Sizes
Android - Use External Profile Image in Notification Bar Like Facebook
Getallcellinfo Returns Null in Android 4.2.1
Why Is Listview.Getcheckeditempositions() Not Returning Correct Values
How to Create Datepicker and Timepicker Dialogs in Fragment Class
Java.Io.Ioexception: Unexpected End of Stream on Connection in Android
Difference Between Android: and App: Prefix in Android Xml
Firebase Authentication Not Working in Signed APK
Mediametadataretriever Setdatasource Throws Illegalargumentexception
How to Integrate Matlab Code Library with Android
How to Get Any Identifier of the Topmost Activity
How to Hide the Share Action (Which Use Most) Icon Near the Share Action Provider