7

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");
}
jatin rana
  • 825
  • 1
  • 9
  • 21

6 Answers6

7

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

Adeel
  • 2,901
  • 7
  • 24
  • 34
jatin rana
  • 825
  • 1
  • 9
  • 21
  • This worked for me in refreshing current fragment. None of the above answers worked in my case. – Zax Sep 29 '21 at 09:51
2

Use this in yout Main Activity:

frag_name frag_name = new frag_name();
                    FragmentManager manager = this.getSupportFragmentManager();
                    manager.beginTransaction().replace(R.id.youtframelayout, frag_name, frag_name.getTag()).commit();

It inicializes the fragment and replaces it in your FrameLayout

Like this your replacing again the fragment :)

Hope it works! :)

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
  • 1
    thanks, i made it work like this. 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(); – jatin rana Mar 17 '17 at 11:11
2

You can simply refresh the current fragment using

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();

Also, in your case you can do something like this,

public void reLoadFragment(Fragment fragment)
{
    Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment");
    Fragment currentFragment = fragment;
    if (currentFragment instanceof "FRAGMENT CLASS NAME") {
        FragmentTransaction fragTransaction =   (getActivity()).getFragmentManager().beginTransaction();
        fragTransaction.detach(currentFragment);
        fragTransaction.attach(currentFragment);
        fragTransaction.commit();
        Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment finish");
    }
    else Log.i(LogGeneratorHelper.INFO_TAG, "fragment reloading failed");
}
Kamesh
  • 1,122
  • 9
  • 12
1

This will refresh current fragment :

  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.detach(this).attach(this).commit();
Sudhir Singh
  • 817
  • 11
  • 16
1

For kotlin use below code.

 fragmentManager!!.beginTransaction().detach(this).attach(this).commit()
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
0

For kotlin:

supportFragmentManager.beginTransaction().detach(fragment).attach(fragment).commit()
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46