Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2x NavHostFragments visible in LayoutInspector, but only 1 in my layouts

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:

enter image description here

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?

like image 890
Sean Blahovici Avatar asked Sep 12 '25 15:09

Sean Blahovici


1 Answers

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.

like image 199
Sean Blahovici Avatar answered Sep 14 '25 05:09

Sean Blahovici