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");
}
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.
You can access another Fragment by its tag: // find your fragment YourFragment f = (YourFragment) getSupportFragmentManager(). findFragmentByTag("yourFragTag"); // update the list view f. updateListView();
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.
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.
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
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