title: Get fragment instance nested inside nav controller from activity
body: <p>My activity has bottom navigation and I would like to get the instance of one of the fragments of the navigation in the activity.</p>
  
  <p>I am trying to get the instance of the <code>com.myapp.ui.MapFragment</code> inside the <code>onCreate</code> of the  activity (after setContentView) by first getting an instance of the <code>nav_host_fragment</code> and then the instance of the <code>com.myapp.ui.MapFragment</code> using <code>childFragmentManager</code>:</p>
  
  <pre><code>val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
  val mapFragment = navFragment!!.childFragmentManager.findFragmentById(R.id.navigation_map)
  </code></pre>
  
  <p>It finds the instance of navFragment but mapFragment is null.</p>
  
  <p>This is the activity layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;LinearLayout
      android:id="@+id/twitter_activity_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:weightSum="100"&gt;
  
      &lt;fragment
          android:id="@+id/nav_host_fragment"
          android:name="androidx.navigation.fragment.NavHostFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:defaultNavHost="true"
          app:layout_constraintBottom_toTopOf="@id/nav_view"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:navGraph="@navigation/mobile_navigation"
          android:layout_weight="90"/&gt;
  
      &lt;com.google.android.material.bottomnavigation.BottomNavigationView
          android:id="@+id/nav_view"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="?android:attr/windowBackground"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          android:layout_weight="10"
          app:menu="@menu/bottom_nav_menu" /&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>This is the navigation layout:</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/mobile_navigation"
  app:startDestination="@+id/navigation_map"&gt;
  
  &lt;fragment
      android:id="@+id/navigation_map"
      android:name="com.myapp.ui.map.MapFragment"
      android:label="@string/title_map"
      tools:layout="@layout/fragment_map" /&gt;
  
  &lt;fragment
      android:id="@+id/navigation_places"
      android:name="com.myapp.ui.places.PlacesContainerFragment"
      android:label="@string/title_places"
      tools:layout="@layout/fragment_places_container" /&gt;
  
  &lt;fragment
      android:id="@+id/navigation_profile"
      android:name="com.myapp.ui.profile.ProfileFragment"
      android:label="@string/title_profile"
      tools:layout="@layout/fragment_profile" /&gt;
  &lt;/navigation&gt;
  </code></pre>
  
  <p>This is the activity's <code>onCreate</code>:</p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
  
      supportActionBar?.hide()
      setTheme(preferencesUtils.appTheme)
  
      setContentView(R.layout.activity_main)
  
      val navController = findNavController(R.id.nav_host_fragment)
      val appBarConfiguration = AppBarConfiguration(
          setOf(R.id.navigation_map, R.id.navigation_places, R.id.navigation_profile)
      )
  
      setupActionBarWithNavController(navController, appBarConfiguration)
  
      nav_view.setupWithNavController(navController)
  
      val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
      val mapFragment = navFragment!!.childFragmentManager.findFragmentById(R.id.navigation_map)
  }
  </code></pre>