fitsSystemWindows effect gone for fragments added via FragmentTransaction

When you use <fragment>, the layout returned in your Fragment’s onCreateView is directly attached in place of the <fragment> tag (you’ll never actually see a <fragment> tag if you look at your View hierarchy.

Therefore in the <fragment> case, you have

DrawerLayout
  CoordinatorLayout
    AppBarLayout
    ...
  NavigationView

Similar to how cheesesquare works. This works because, as explained in this blog post, DrawerLayout and CoordinatorLayout both have different rules on how fitsSystemWindows applies to them – they both use it to inset their child Views, but also call dispatchApplyWindowInsets() on each child, allowing them access to the fitsSystemWindows="true" property.

This is a difference from the default behavior with layouts such as FrameLayout where when you use fitsSystemWindows="true" is consumes all insets, blindly applying padding without informing any child views (that’s the ‘depth first’ part of the blog post).

So when you replace the <fragment> tag with a FrameLayout and FragmentTransactions, your view hierarchy becomes:

DrawerLayout
  FrameLayout
    CoordinatorLayout
      AppBarLayout
      ...
  NavigationView

as the Fragment’s view is inserted into the FrameLayout. That View doesn’t know anything about passing fitsSystemWindows to child views, so your CoordinatorLayout never gets to see that flag or do its custom behavior.

Fixing the problem is actually fairly simple: replace your FrameLayout with another CoordinatorLayout. This ensures the fitsSystemWindows="true" gets passed onto the newly inflated CoordinatorLayout from the Fragment.

Alternate and equally valid solutions would be to make a custom subclass of FrameLayout and override onApplyWindowInsets() to dispatch to each child (in your case just the one) or use the ViewCompat.setOnApplyWindowInsetsListener() method to intercept the call in code and dispatch from there (no subclass required). Less code is usually the easiest to maintain, so I wouldn’t necessarily recommend going these routes over the CoordinatorLayout solution unless you feel strongly about it.

Leave a Comment