在LayoutInspector中可见2x NavHostFragments,但在我的布局中仅1

问题描述

我遇到了这个问题,对我来说似乎很奇怪。通过布局检查器查看我的应用程序时,由于某些原因,我可以看到navHostFragment嵌套在自身内部:

enter image description here

但是,这是我的布局文件的一部分:

<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"/>

我没有在片段容器视图中为另一个navHostFragment充气,那么为什么navHostFragment在布局检查器中重复?这是正常现象还是某种隐藏的navHostFragment被夸大了?

解决方法

通过进一步测试,似乎使用

由于FragmentContainerView是推荐的容器,因此我认为重复是不可避免的。

我不知道这是否可以视为Fragments中的错误,还是预期的行为。