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"); } 

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me in refreshing current fragment. None of the above answers worked in my case.
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"); } 

Comments

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! :)

1 Comment

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();
1

This will refresh current fragment :

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

Comments

1

For kotlin use below code.

 fragmentManager!!.beginTransaction().detach(this).attach(this).commit() 

Comments

0

For kotlin:

supportFragmentManager.beginTransaction().detach(fragment).attach(fragment).commit() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.