Clear backstack without loops
String backStackEntryNamename = getSupportFragmentManager().getBackStackEntryAt(0).getName(); getSupportFragmentManager().popBackStack(name, FragmentManager.POP_BACK_STACK_INCLUSIVE); Where name is the addToBackStack() parameter
getSupportFragmentManager().beginTransaction(). .replace(R.id.container, fragments.get(titleCode)) .addToBackStack(name)