This is the function responsible for adding fragments to back stack:
public void populateContent(File f)
{
ContentFragment cf = new ContentFragment(ctx, ac,
I was adding, not replacing, but I had to call addToBackStack(null) before add(), not after, on the fragment which will close, not on the fragment which will stay open.
In class A (opened first)
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.layout_a, f, Constants.FRAGMENT_KEY);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
In class B (opened by class A)
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.layout_b, f, Constants.FRAGMENT_KEY);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();