There are actually two things you should do to resolve this issue:
1. Make sure you use getChildFragmentManager() NOT getFragmentManager() when launching FragmentB from FragmentA
Calling getChildFragmentManager() will return the hosting Fragment’s FragmentManager, whereas getFragmentManager() will return the hosting Activity’s FragmentManager. It’s important to use getChildFragmentManager() because you’re nesting a Fragment inside another Fragment, so the parent Fragment should be in charge of handling any transactions with the nested Fragment. If you use getFragmentManager(), you’ll run into the issue you’re experiencing right now.
2. DO NOT use setTargetFragment() and getTargetFragment(), they will not work when using getChildFragmentManager()
Instead, use getParentFragment(). I believe there is some kind of bug in Android right now where even if you properly call
fragmentB.setTargetFragment(fragmentA, 0);
and then show FragmentB, after a configuration change, calling getTargetFragment() from FragmentB will return itself instead of FragmentA.