title: Fragment has not been attached yet
body: <p>After reading posts on this exception, I can't understand what I need to do to correct this error. Besides, I can't even reproduce it... This happens sometimes on some devices but I don't understand how...</p>
  
  <p>my logs : </p>
  
  <pre class="lang-java prettyprint-override"><code>Fatal Exception: java.lang.IllegalStateException
  Fragment a has not been attached yet my.app.HostFragment.addFragment
  </code></pre>
  
  <p>HostFragment class :</p>
  
  <pre class="lang-java prettyprint-override"><code>fun addFragment(fragment: Fragment) {
          childFragmentManager.beginTransaction()
            .add(R.id.fragment_root_container, fragment)
            .addToBackStack(null)
            .commit()
  }
  </code></pre>
  
  <p>MainActivity class :</p>
  
  <pre class="lang-java prettyprint-override"><code>fun openNewChampionFragment() {
          val hostFragment = pagerAdapter.getItem(viewpager.currentItem) as HostFragment
          hostFragment.addFragment(ChampionFragment.newInstance())
  }
  </code></pre>
  
  <p>ViewPager Adapter :</p>
  
  <pre class="lang-java prettyprint-override"><code>class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
  
      private var fragments: MutableList&lt;Fragment&gt; = mutableListOf()
  
      fun addFragments(fragments: List&lt;Fragment&gt;) {
          this.fragments = fragments.toMutableList()
      }
  
      override fun getItem(position: Int): Fragment {
          return fragments[position]
      }
  
      override fun getCount(): Int {
          return fragments.size
      }
  
      fun clearStack(index: Int){
          (fragments[index] as HostFragment).clearAllStack()
      }
  }
  </code></pre>
  
  <p>I always call <code>addFragment</code> from my <code>MainActivity</code> with a new fragment instance (I don't reuse the instance of the old fragments.)</p>
  
  <p>Which fragment has not been attached ? My HostFragment or the new one that i'm trying to add.</p>