I have situation where I want to clear all fragments from backstack except the one which is visible(i.e. on top)
For example, There are four fragments in backstack A->B->C->D (D is on top)
Now I want to remove fragments A,B,C from backstack. But the constraint is there should not be any visible effect on fragment D while removing history from backstack.
This is my code.
FragmentManager fm = getActivity().getSupportFragmentManager();
Bundle bundle = new Bundle();
OrderReceiptFragment orderReceiptFragment = new OrderReceiptFragment();
bundle.putSerializable("orderHistory", orderHistory);
orderReceiptFragment.setArguments(bundle);
CommonUtil.clearBackstack(fm);
fm.beginTransaction().setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right)
.replace(R.id.container, orderReceiptFragment).commit();
method to clearbackstack
public static void clearBackstack(FragmentManager fragmentManager) {
fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
Here the problem is - while clearing the backstack, for some milliseconds the first fragment from the backstack gets visible. Which looks weird. Does anybody have any solution for this?