App crashes on adding second navHostFragment

1

Due to a condition, I need two navHostFragment in two different activities in my app. Everything works fine until I add a second navhostframent in my app, which creates an inflateexception with error:

Binary XML file line #36: Duplicate id 0x7f09018a, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment

Following is my second navhost fragment:

    <fragment
                android:id="@+id/nav_host_fragment_supervisor"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/bottomNav"
                android:layout_below="@+id/appbar"
                app:defaultNavHost="true"
                app:navGraph="@navigation/navigation_graph_supervisor" />

Please help.

java
android
android-layout
kotlin
android-fragments
asked on Stack Overflow Sep 10, 2020 by Shikhar • edited Sep 10, 2020 by jmizv

1 Answer

0

The problem is the way you have used <fragment>. You can include <fragment> in XML to embed fragment instance inside of the layout. What I can suggest is to create a whole new layout for your fragment and not use <fragment>. EDIT Create a separate layout and then embed your fragment instance inside it for example:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <fragment
            android:id="@+id/nav_host_fragment_supervisor"
           android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottomNav"
            android:layout_below="@+id/appbar"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation_graph_supervisor" />

</FrameLayout>
answered on Stack Overflow Sep 10, 2020 by Akshay Shenoy • edited Sep 11, 2020 by Akshay Shenoy

User contributions licensed under CC BY-SA 3.0