Reload Fragment

2019-02-26 17:38发布

问题:

I am just curious, as I have set up Fragment tabs in a FragmentActivity, how to reload a fragment either from the FragmentActivity, or from the Fragment itself, to just purge and reload that tab. I essentially need the same thing that happens when I reselect the tab to happen, when it replaces the fragment. Is there a simple way to do this and reload the fragment ?

回答1:

Create a method that begins a FragmentTransaction, detaches the fragment, and commits. Then begin a new FragmentTransaction which attachs the fragment and commits. This should destroy the view hierarchy, create it again, attach it to the UI and display it. Whenever you need the fragment refreshed, jsut call this method.



回答2:

You can do something like this:

fm=getActivity().getSupportFragmentManager();
{
fm.beginTransaction().replace(Containername, new CurrentFragmentname, CurrentFragment.class.getName()).commit();
}