According to I have an Activity which contains ViewPager which loads three fragments:
- RootA
- RootB
- RootC
RootA loads another fragment which named as FragA.
RootB loads another fragment which named as FragB.
RootC loads another fragment which named as FragC.
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.container, new FragA());
fragTransaction.commit();
FragA has a button named as ButtonA to load FragAA.
FragB has a button named as ButtonB to load FragBB.
FragC has a button named as ButtonC to load FragCC.
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
fragTransaction.replace(R.id.container, new FragAA, "FragAA");
fragTransaction.addToBackStack("FragAA");
fragTransaction.commit();
I run my App and in the first tab, I press ButtonA. FragAA will be shown. Now I swipe to the second tab and press ButtonB and go to FragBB and then I swipe to the third tab and press ButtonC and go to FragCC.
After that, I swipe to the second tab and then I swipe to the first tab again without any clicking. then I press Back Button. By the first back pressing FragCC will be closed (will put out of BackStack), by the second back pressing FragBB will be closed, and by the third back pressing FragAA will be closed.
But I want my app to close child of current tab not to close fragments according to the BackStack.
I mean the Back Button should close FragAA because I am in the first tab and I pressed Back Button in the first tab!
How can I do it?