I have had this issue that seems kind of weird to me. When looking at my app through the Layout Inspector, I can see that my navHostFragment is nested inside itself for some reason:
However, here is what part of my layout file looks like:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".core.MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/activity_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I do not inflate another navHostFragment in the fragment container view, so why is the navHostFragment duplicated in layout inspector? Is this somehow normal or is there a hidden navHostFragment being inflated somehow?
Well upon further testing it seems like this additional layer is generated when using <androidx.fragment.app.FragmentContainerView but isn't there when using the <fragment tag.
Since the FragmentContainerView is the recommended container, I assume the duplicate is unavoidable.
I do not know if this could be considered a bug in Fragments or if it is expected behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With