I have ViewPager that has 3 tabs. Tab(3) includes 2 fragments (A) --> (B).
Tab(1)
Tab(2)
Tab(3) --> (B) --> (C)
The way I replace fragments in Tab(3):
FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();
trans.replace(R.id.B_container, new FragmentC().newInstance(), "C");
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.addToBackStack("B->C");
trans.commit();
When I'm in (B) and I'm hitting a button it takes me straight to Tab(3), skipping (A). Here is the code from button in (B):
FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
Everything works good. The problem is when I'm in (B) but then I go to Tab(1) or Tab(2) and hit the button after it doesn't work. Please let me know what I'm doing wrong. Thank you.
//======================================================================
I tried to replace etActivity().getSupportFragmentManager() with getChildFragmentMaganer(). Since the regular way of popBackStack() wasn't working anymore I had to modify it. Now it works and I have the same problem tho.
FragmentManager fm = getActivity().getSupportFragmentManager();
for (Fragment frag : fm.getFragments()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
childFm.popBackStack();
}
}
}