I have some fragments which will be replaced by following method. I think there's something wrong with my code because I want to prevent from adding multiple times a fragment into the back stack. If I click on fragment B twice, all instances will be added to the back stack and pressing back button will be passed through the two created instances.
public void replaceFragment(Fragment fragment, boolean addToBackStack, boolean customAnimation) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); String tag = fragment.getClass().getSimpleName(); if (customAnimation) { transaction.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_bottom, R.anim.slide_in_bottom, R.anim.slide_out_bottom); } transaction.replace(R.id.fragment_container, fragment, tag); // remove from back stack if exists // always return false! boolean f = manager.popBackStackImmediate(tag, 0); if (addToBackStack) { transaction.addToBackStack(tag); } transaction.commit(); }