FragmentManager popBackStack doesn't remove fragment

☆樱花仙子☆ 提交于 2019-11-30 05:15:11

I found that the problem was not in the logic of adding and removing fragment of the stack.

The problem was that some of the fragment loaded another fragments inside of it (it had ViewPager component). Then I thought that when the fragment was removed then these fragments were removed too.

This is true ONLY if you use getChildFragmentManager() method. This method MUST be used when loading fragments inside other fragmets. If not, then the fragments are asociated with the fragments activity.

I found this question, because after calling

fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

this code fragmentManager.getFragments().size() returns the maximum number of fragments me, that were in the stack. I checked every fragment on null. And I found that some fragment is null on my case. Maybe it will help to somebody)

popBackStack will just revert your last FragmentTransaction.

If you use FragmentTransaction.add, popBackStack will just call FragmentTransacetion.remove.

But if you call FragmentTransaction.replace, popBackStack will call FragmentTransaction.remove and FragmentTransaction.add

For your "NOTE 1" : FragmentTransaction.replace will not change your fragment state.

zIronManBox

If you are really looking to remove fragments at once then follow: How to replace Fragments of different types?

Otherwise use replace transaction for fragments to smooth transitiona and hassel free approach, see https://stackoverflow.com/a/23013075/3176433

Also understand Fragment lifecycle, http://developer.android.com/guide/components/fragments.html

I had a similar problem where the popBackStack() didn't remove my fragment. However, I noticed that I called the wrong FragmentManager, where I had to call getSupportFragmentMananger() instead of getFragmentManager().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!