Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a fragment android

Earlier this code was working but it now it suddenly stopped working. Fragment is not detaching from parent activity.

public void reLoadFragment(Fragment fragment)
{
    Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment");
    // Reload current fragment
    Fragment frg = null;
    frg = getSupportFragmentManager().findFragmentByTag(fragment.getClass().getName());
    frg.onDetach();
    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.detach(frg);
    ft.attach(frg);
    ft.commit();
    Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment finish");
}
like image 255
jatin rana Avatar asked Mar 17 '17 10:03

jatin rana


People also ask

How to refresh fragment data?

Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so. Save this answer.

How do I refresh a fragment from another fragment?

You can access another Fragment by its tag: // find your fragment YourFragment f = (YourFragment) getSupportFragmentManager(). findFragmentByTag("yourFragTag"); // update the list view f. updateListView();

How do I refresh a ViewPager fragment?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

What is findFragmentByTag?

This method allows you to retrieve the instance of a previously added fragment with the given tag regardless of the container it was added to.


1 Answers

ft.detach() not working after support library update 25.1.0. This solution works fine after update:

getSupportFragmentManager().beginTransaction().detach(oldFragment).commitNowAllowingStateLoss();
getSupportFragmentManager().beginTransaction().attach(oldFragment).commitAllowingStateLoss(); 

Credit: Refresh or force redraw the fragment

like image 192
jatin rana Avatar answered Sep 29 '22 07:09

jatin rana