I\'m wondering if this is actually a bug in the Android API:
I have a setup like so:
┌----┬---------┐
| | |
| 1 | 2 |
| |┌------
If you find your nested fragment not being removed or being duplicated (eg. on Activity restart, on screen rotate) try changing:
transaction.add(R.id.placeholder, newFragment);
to
transaction.replace(R.id.placeholder, newFragment);
If above doesn't help, try:
Fragment f = getChildFragmentManager().findFragmentById(R.id.placeholder);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (f == null) {
Log.d(TAG, "onCreateView: fragment doesn't exist");
newFragment= new MyFragmentType();
transaction.add(R.id.placeholder, newFragment);
} else {
Log.d(TAG, "onCreateView: fragment already exists");
transaction.replace(R.id.placeholder, f);
}
transaction.commit();
Learnt here