title: How to add NavHostFragment in a Fragment destination?
body: <p>My goal is to understand and use the Navigation component in the best way with a complex navigation architecture (I suppose).</p>
  
  <p>To make the global context, I use a main BottomNavigationBar with 5 items. For the management of the fragments associate, I use the example given by Google : <a href="https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample" rel="nofollow noreferrer">https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample</a>.</p>
  
  <p>But my case is a bit more complex than just a bottom bar. In one of the destinations from one of the fragments launched by the bottom bar, I have another RecyclerView menu and a fragment container to navigate between fragments. I put here the important part of my primary graph : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;navigation 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/main"
      app:startDestination="@id/mainFragment"&gt;
      &lt;fragment
          android:id="@+id/mainFragment"
          android:name="com.exemple.fragment.MainFragment"
          android:label="@string/main_label"
          tools:layout="@layout/mainFragment"&gt;
          &lt;action
              android:id="@+id/action_mainFragment_to_goScreen"
              app:destination="@id/goScreen"&gt;
          &lt;/action&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id="@+id/goScreen"
          android:name="com.exemple.fragment.GoFragment"
          android:label="@string/goLabel"
          tools:layout="@layout/fragment_go"&gt;
          &lt;action
              android:id="@+id/action_goScreen_to_mainFragment"
              app:destination="@id/mainFragment" /&gt;
          &lt;argument
              android:name="arg1"
              app:argType="com.exemple.customType"
              android:defaultValue="@null"
              app:nullable="true" /&gt;
          &lt;argument
              android:name="arg2"
              app:argType="string"
              android:defaultValue="@null"
              app:nullable="true" /&gt;
      &lt;/fragment&gt;
  &lt;/navigation&gt;
  </code></pre>
  
  <p>And the xml with the bottom bar which starts this graph : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout 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"
      tools:context="com.exemple.MainActivity"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/constraintLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;androidx.appcompat.widget.Toolbar
              android:id="@+id/fake_toolbar"
              android:layout_width="0dp"
              android:layout_height="0dp"/&gt;
  
          &lt;androidx.fragment.app.FragmentTabHost
              android:id="@+id/main_host_container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@+id/bottom_navigation" /&gt;
  
          &lt;!-- navigation bottom --&gt;
          &lt;com.google.android.material.bottomnavigation.BottomNavigationView
              android:id="@+id/bottom_navigation"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="start"
              android:layout_alignParentBottom="true"
              android:background="@color/navigationBarColor"
              app:itemIconTint="@color/textColorNavigationBar"
              app:itemTextColor="@color/textColorNavigationBar"
              app:menu="@menu/menu_bottom_navigation" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>And here is an example of the graph corresponding to the go fragment (the destination ): </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;navigation 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/go"
      app:startDestination="@id/firstGoFragment"&gt;
  
      &lt;action android:id="@+id/action_to_firstGoFragment"
          app:destination="@id/firstGoFragment"/&gt;
      &lt;fragment
          android:id="@+id/firstGoFragment"
          android:name="com.exemple.go.firstGoFragment"
          android:label="@string/firstGoFragmentLabel"
          tools:layout="@layout/firstGoFragment" &gt;
      &lt;/fragment&gt;
  
      &lt;action android:id="@+id/action_to_secondFragment"
      app:destination="@id/secondFragment"/&gt;
      &lt;fragment
          android:id="@+id/secondFragment"
          android:name="com.exemple.go.SecondFragment"
          android:label="@string/secondFragmentLabel"
          tools:layout="@layout/secondFragment" &gt;
      &lt;/fragment&gt;
  &lt;/navigation&gt;
  </code></pre>
  
  <p>The associate xml is : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:tools="http://schemas.android.com/tools"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/colorGreyLight"
          android:clickable="true"
          android:focusable="true"&gt;
  
          &lt;RelativeLayout
              android:id="@+id/linearLayout3"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="@color/colorPrimary"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"&gt;
  
              &lt;androidx.appcompat.widget.AppCompatImageView
                  android:id="@+id/quit"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerVertical="true"
                  android:layout_marginStart="16dp"
                  android:paddingBottom="15dp"
                  android:paddingEnd="15dp"
                  android:paddingTop="15dp"
                  app:srcCompat="@drawable/ic_quit_white" /&gt;
  
              &lt;com.teedji.mobeelity.custom.CustomTextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:text="@string/go"
                  android:textColor="@color/colorWhite"
                  android:textSize="17sp"
                  android:textStyle="bold" /&gt;
  
          &lt;/RelativeLayout&gt;
  
          &lt;androidx.recyclerview.widget.RecyclerView
              android:id="@+id/goMenu"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:background="@color/colorWhite"
              android:overScrollMode="never"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="1.0"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/linearLayout3" /&gt;
  
          &lt;!-- include fragment child container --&gt;
          &lt;fragment
              android:id="@+id/go_nav_host_fragment_container"
              android:name="androidx.navigation.fragment.NavHostFragment"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_marginTop="0dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/goMenu"
              app:navGraph="@navigation/go"/&gt;
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>Now, to navigate to the go screen I simply use </p>
  
  <pre><code>findNavController().navigate(MainFragmentDirections.actionMainFragmentToGoScreen(arg1, arg2))
  </code></pre>
  
  <p>or </p>
  
  <pre><code>findNavController().navigate(R.id. action_mainFragment_to_goScreen)
  </code></pre>
  
  <p>But when I am on the Go fragment, the navController the I find using <code>findNavController()</code> is still the mainFragment one. So the <code>navigate()</code> fonction didn't find the action id. To solve it, I had to change the navHost programaticaly in the GoFragment like this :</p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
          Log.i(LOG_TAG, "onCreate")
          super.onCreate(savedInstanceState)
          if( fragmentManager != null ) {
              // If the Nav Host fragment exists, return it
              val existingFragment = fragmentManager!!.findFragmentByTag(FRAGMENT_TAG) as NavHostFragment?
              existingFragment?.let { navHostGoFragment = it }
  
              // Otherwise, create it and return it.
              if ( navHostGoFragment == null) {
                  navHostGoFragment = NavHostFragment.create(R.navigation.go)
                  fragmentManager!!.beginTransaction()
                          .add(R.id.go_nav_host_fragment_container, navHostGoFragment!!, FRAGMENT_TAG)
                          .commit()
              }
          }else {
              Log.e(LOG_TAG, "fragmentManager is null so I can't find the fragment host")
          }
  }
  </code></pre>
  
  <p>and then use <code>navHostGoFragment!!.navController.navigate(R.id.action_to_firstGoFragment)</code> to navigate in the new container
  But doing like this, my firstFragment is created twice (I can see it on my logs) and that generate some problems like <code>Cannot add the same observer with different lifecycles</code> for example ( I solve this with adding</p>
  
  <pre><code>if  (model.getLiveData().hasObservers()) {
          model.getLiveData().removeObserver(observer)
      }
  </code></pre>
  
  <p>before </p>
  
  <pre><code>model.getLiveData().observe(viewLifecycleOwner, observer)
  </code></pre>
  
  <p>)
  But I think I am not doing the Navigation part like I should...</p>
  
  <p>Could you provide any help to make this work more efficiently with stability ?</p>