title: Fragments not added to backstack using Navigation Components
body: <h2>Information:</h2>
  
  <p>I'm programmatically inserting a <code>NavHostFragment</code> for each feature of the app. Each <code>NavHostFragment</code> has it's own Navigation Graph. Dagger is providing them by using a <code>FragmentFactory</code> specific to each feature. It's a <strong>Single Activity</strong> structure with MVVM architecture.</p>
  
  <p>Repo: <a href="https://github.com/mitchtabian/DaggerMultiFeature/tree/nav-component-backstack-bug" rel="noreferrer">https://github.com/mitchtabian/DaggerMultiFeature/tree/nav-component-backstack-bug</a></p>
  
  <p><strong>checkout the branch "nav-component-backstack-bug".</strong></p>
  
  <hr>
  
  <h2>The Problem</h2>
  
  <p>When navigating into the graph the fragments are not being added to the backstack. The only fragment that's added is whichever one has most recently been visited. <strong>So the stack size always stays at one.</strong></p>
  
  <p>Originally I thought it was because I wasn't setting the <code>FragmentFactory</code> to the <code>ChildFragmentManager</code> but that doesn't change anything. See the code snippets below for the relevant code. Or checkout the project and run it. I have logs printing out the fragments currently in the backstack from the <code>ChildFragmentManager</code> and also the <code>SupportFragmentManager</code>. Both have a constant size of 1.</p>
  
  <hr>
  
  <p><strong>Feature1NavHostFragment.kt</strong></p>
  
  <p>This is one of the custom <code>NavHostFragment</code>'s. The <code>create()</code> function in the companion object is how I create them.</p>
  
  <pre class="lang-java prettyprint-override"><code>
  class Feature1NavHostFragment
  @Inject
  constructor(
      private val feature1FragmentFactory: Feature1FragmentFactory
  ): NavHostFragment(){
  
      override fun onAttach(context: Context) {
          ((activity?.application) as BaseApplication)
              .getAppComponent()
              .feature1Component()
              .create()
              .inject(this)
          childFragmentManager.fragmentFactory = feature1FragmentFactory
          super.onAttach(context)
      }
  
      companion object{
  
          const val KEY_GRAPH_ID = "android-support-nav:fragment:graphId"
  
          @JvmStatic
          fun create(
              feature1FragmentFactory: Feature1FragmentFactory,
              @NavigationRes graphId: Int = 0
          ): Feature1NavHostFragment{
              var bundle: Bundle? = null
              if(graphId != 0){
                  bundle = Bundle()
                  bundle.putInt(KEY_GRAPH_ID, graphId)
              }
              val result = Feature1NavHostFragment(feature1FragmentFactory)
              if(bundle != null){
                  result.arguments = bundle
              }
              return result
          }
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>MainActivity.kt</strong></p>
  
  <p>This is an example of how I create the <code>NavHostFragment</code>'s in MainActivity.</p>
  
  <pre class="lang-java prettyprint-override"><code>
  val newNavHostFragment = Feature1NavHostFragment.create(
                                    getFeature1FragmentFactory(),
                                    graphId
                           )
  supportFragmentManager.beginTransaction()
      .replace(
          R.id.main_nav_host_container,
          newNavHostFragment,
          getString(R.string.NavHostFragmentTag)
      )
      .setPrimaryNavigationFragment(newNavHostFragment)
      .commit()
  
  </code></pre>
  
  <hr>
  
  <p><strong>Feature1MainFragment.kt</strong></p>
  
  <p>And here is an example of how I'm navigating to other fragments in the graph.</p>
  
  <pre class="lang-java prettyprint-override"><code>
  btn_go_next.setOnClickListener {
      findNavController().navigate(R.id.action_feature1MainFragment_to_feature1NextFragment)
  }
  
  </code></pre>
  
  <hr>
  
  <h1>Summary</h1>
  
  <p>Like I said, in every fragment I'm printing the backstack for both the <code>ChildFragmentManager</code> and the <code>SupportFragmentManager</code>. Both have a constant size of one. It's as if the fragments are being replaced as I navigate into the graph instead of added to the stack. </p>
  
  <p>Anyways, thanks for reading this and I would appreciate any insights.</p>