Fragment overlap is one of those Android bugs that makes you question your sanity. Two fragments visible at the same time, stacked on top of each other. Here’s what causes it and how to fix it.

The Scenario

You want to navigate A → B → C, but skip B from the back stack so pressing back from C returns directly to A.

The intuitive approach is to skip addToBackStack() on the B→C transaction:

// A → B (add to back stack)
fragmentManager.beginTransaction()
    .replace(R.id.container, fragmentB)
    .addToBackStack(null)
    .commit();

// B → C (skip back stack - THIS CAUSES PROBLEMS)
fragmentManager.beginTransaction()
    .replace(R.id.container, fragmentC)
    .commit();  // No addToBackStack()

Why This Breaks

When you press back from C, the fragment manager tries to reverse the last back stack entry. But that entry knows about A→B, not B→C.

The reverse operation attempts to:

  1. Remove B (which isn’t displayed!)
  2. Re-add A

Result: Both A and C are visible simultaneously.

The Root Cause

The fragment manager maintains an internal list of fragments. When back stack operations reverse without proper tracking, fragments get duplicated in this list. Multiple fragments with the same container ID = overlap.

The Fix

Always add transactions to the back stack, then handle custom navigation logic manually:

// Always add to back stack
fragmentManager.beginTransaction()
    .replace(R.id.container, fragmentC)
    .addToBackStack("C")
    .commit();

// Handle custom back navigation
getSupportFragmentManager().addOnBackStackChangedListener(() -> {
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        String name = fragmentManager.getBackStackEntryAt(count - 1).getName();
        if ("B".equals(name)) {
            // Skip B, pop again
            fragmentManager.popBackStackImmediate();
        }
    }
});

Key Takeaway

Don’t fight the fragment manager’s internal state. Let it track everything, then use popBackStackImmediate() and listeners to implement custom navigation flows. Your fragments will thank you.