I have an activity with 4 fragments. I would like to avoid situations where user click 100 times before exit. When a fragment is set, it's added to back stack. For example: User sets fragments in this order: 1, 2, 3, 4 then he sets frag 2 and then 4. My backStack looks like that (fragment 4 is visible): 2, 4, 3, 2, 1 and exit. I would like that my backStack looks like: 2, 3, 1 and exit.
I've tried to solve my problem with popBackStackImmediate(string, int flags), like in this post: Prevent The Same Fragment From Stacking More Than Once ( addToBackStack) But my backStack looks like (with 4 visible) 2, 1 and exit. I deduced that popBackStackImmediate() is removing stacks on top of the stack which is popped (I hope you know what I mean).
private void setFragment(Fragment fragment) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_frame, fragment);
ft.addToBackStack(backStateName);
mMainNav.setItemBackgroundResource(R.color.PopWindowBorder_HomeRenovationfragment);
ft.commit();
}
}
I expect to have each fragments no more than one time in the back stack without disrupting the order of the backStack in order to have a back stack which looks like that: 2, 3, 1 and exit. :)