title: Fragment disappears with ViewPager2 and FragmentStateAdapter
body: <p>I'm building an app using <code>ViewPager2</code> that displays different fragments when swiped. My adapter extends <code>FragmentStateAdapter</code> as described <a href="https://developer.android.com/jetpack/androidx/releases/viewpager2" rel="noreferrer">here</a>. </p>
  
  <p>When I put my app into the background and then resume again, the current fragment disappears. What do I need to do to make sure the current fragment is restored correctly?</p>
  
  <p>(Note: I am asking about <code>FragmentStateAdapter</code>, not <code>FragmentPagerAdapter</code> or <code>FragmentStatePagerAdapter</code>.)</p>
  
  <p><strong>Update 1</strong>:</p>
  
  <p>My fragment class is as follows:</p>
  
  <pre><code>class PagerFragment : Fragment() {
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      val layoutResId: Int = arguments!!.getInt(ARG_LAYOUT_RES_ID)
      return inflater.inflate(layoutResId, container, false)
  }
  
  companion object {
      private const val ARG_LAYOUT_RES_ID = "layoutResId"
  
      fun newInstance(layoutResId: Int): PagerFragment {
          val args = Bundle()
          args.putInt(ARG_LAYOUT_RES_ID, layoutResId)
  
          val fragment = PagerFragment()
          fragment.arguments = args
          return fragment
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I pass in the desired layout resource id for each fragment when it is created.</p>
  
  <p>My view pager adapter class is as follows:</p>
  
  <pre><code>class ViewPagerAdapter(fm: FragmentManager) : FragmentStateAdapter(fm) 
  {
      override fun getItem(position: Int): Fragment {
          return PagerFragment.newInstance(Helper.getLayoutResId(position))
      }
  
      override fun getItemCount(): Int {
          return Helper.getSize()
      }
  }
  </code></pre>
  
  <p>Adding <code>retainInstance = true</code> does not resolve the problem.</p>
  
  <p><strong>Update 2</strong>:</p>
  
  <p>Calling <code>notifyDataSetChanged()</code> in <code>onResume()</code> in my main activity solves the problem for now, although it feels like a heavier hammer than should be necessary. (The original <code>ViewPager</code> + <code>FragmentStatePagerAdapter</code> does not require this type of call to reload fragments properly after bringing in the app from the background.)</p>
  
  <p><strong>Update 3</strong>:</p>
  
  <p>Updating my dependency from <code>androidx.viewpager2:viewpager2:1.0.0-alpha01</code> to the recently released <code>androidx.viewpager2:viewpager2:1.0.0-alpha02</code> fixes the issue entirely :).</p>