title: ViewPager2 doesn't show correct Fragment on slow devices or when debugging
body: <p>I'm using <code>ViewPager2</code> with <code>TabLayout</code> and <code>FragmentStateAdapter</code> to display Fragments in tabs. I found out that when I run it on slower devices (ie. Nexus 5X) or when I put a breakpoint to <code>createFragment()</code> method, after I change to some later tab with Fragment, that is not created yet, Fragment on last position is shown.</p>
  <p>In example I have 20 items. I select tab on position 15. Fragments on positions 15, 14, 16, 17, 18, 19 and 20 are created (<code>createFragment()</code> is called). I would expect to create only 14, 15, 16 to be created. On slow devices or when stopping debugger inside <code>createFragment()</code> method, Fragment on position 20 is shown.</p>
  <p>This issue is not happening on the devices that are not slowed down.</p>
  <p><strong>MainActivity:</strong></p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      initViewPager2WithFragments()
  }
  
  private fun initViewPager2WithFragments() {
      val viewPager2: ViewPager2 = findViewById(R.id.viewpager)
      val adapter = ExampleStateAdapter(supportFragmentManager, lifecycle)
      viewPager2.adapter = adapter
  
      val tablayout: TabLayout = findViewById(R.id.tablayout)
      val names: Array&lt;String&gt; = arrayOf(
          &quot;Fragment A&quot;,
          &quot;Fragment B&quot;,
          &quot;Fragment C&quot;,
          &quot;Fragment D&quot;,
          &quot;Fragment E&quot;,
          &quot;Fragment F&quot;,
          &quot;Fragment G&quot;,
          &quot;Fragment H&quot;,
          &quot;Fragment I&quot;,
          &quot;Fragment J&quot;,
          &quot;Fragment K&quot;,
          &quot;Fragment L&quot;,
          &quot;Fragment M&quot;,
          &quot;Fragment N&quot;
      )
      TabLayoutMediator(tablayout, viewPager2) { tab, position -&gt;
          tab.text = names[position]
      }.attach()
  }
  }
  </code></pre>
  <p><strong>ExampleStateAdapter:</strong></p>
  <pre><code>class ExampleStateAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
      FragmentStateAdapter(fragmentManager, lifecycle) {
  
      override fun getItemCount(): Int {
          return 14
      }
  
      override fun createFragment(position: Int): Fragment {
          Log.d(&quot;frg&quot;, &quot;createFragment: $position&quot;)
          return when (position) {
              0 -&gt; SomeFragment.newInstance(&quot;A&quot;)
              1 -&gt; SomeFragment.newInstance(&quot;B&quot;)
              2 -&gt; SomeFragment.newInstance(&quot;C&quot;)
              3 -&gt; SomeFragment.newInstance(&quot;D&quot;)
              4 -&gt; SomeFragment.newInstance(&quot;E&quot;)
              5 -&gt; SomeFragment.newInstance(&quot;F&quot;)
              6 -&gt; SomeFragment.newInstance(&quot;G&quot;)
              7 -&gt; SomeFragment.newInstance(&quot;H&quot;)
              8 -&gt; SomeFragment.newInstance(&quot;I&quot;)
              9 -&gt; SomeFragment.newInstance(&quot;J&quot;)
              10 -&gt; SomeFragment.newInstance(&quot;K&quot;)
              11 -&gt; SomeFragment.newInstance(&quot;L&quot;)
              12 -&gt; SomeFragment.newInstance(&quot;M&quot;)
              13 -&gt; SomeFragment.newInstance(&quot;N&quot;)
              else -&gt; SomeFragment.newInstance(&quot;0&quot;)
          }
      }
  }
  </code></pre>
  <p><strong>SomeFragment:</strong></p>
  <pre><code>class SomeFragment : Fragment() {
  
      var TAG = &quot;frg 0&quot;
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
          TAG = &quot;frg ${arguments?.getString(CATEGORY)}&quot;
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_some, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          view.findViewById&lt;TextView&gt;(R.id.textView).apply {
              text = arguments?.let {
                  it.getString(CATEGORY, &quot;&quot;)
              } ?: &quot;not found&quot;
          }
      }
  
      override fun onStart() {
          Log.d(TAG, &quot;onStart()&quot;)
          super.onStart()
      }
  
      override fun onResume() {
          Log.d(TAG, &quot;onResume()&quot;)
          super.onResume()
      }
  
      override fun onPause() {
          Log.d(TAG, &quot;onPause()&quot;)
          super.onPause()
      }
  
      override fun onStop() {
          Log.d(TAG, &quot;onStop()&quot;)
          super.onStop()
      }
  
      companion object {
  
          fun newInstance(
              categoryName: String
          ) = SomeFragment().apply {
              arguments = bundleOf(
                  CATEGORY to categoryName
              )
          }
      }
  }
  </code></pre>
  <p><strong>fragment_some.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/ConstraintLayout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;#CDDC39&quot;
      tools:context=&quot;.fragments.SomeFragment&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Fragment&quot;
          android:textSize=&quot;30sp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><strong>activity_main.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  
      &lt;androidx.viewpager2.widget.ViewPager2
          android:id=&quot;@+id/viewpager&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;0dp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/tablayout&quot; /&gt;
  
  
      &lt;com.google.android.material.tabs.TabLayout
          android:id=&quot;@+id/tablayout&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/btn&quot;
          app:tabMode=&quot;scrollable&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><strong>dependencies:</strong></p>
  <pre><code>// ViewPager2
  implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
  // CardView
  implementation 'androidx.cardview:cardview:1.0.0'
  // Tablayout + other stuff...
  implementation 'com.google.android.material:material:1.2.1'
  </code></pre>
  <p>I tried to put some logs and see what's happening. I have 14 Fragments in the list and I select tab on position 9.</p>
  <p><strong>Normal scenario</strong> (not stopping debugger in <code>createFragment()</code> method):</p>
  <pre><code>2020-09-22 15:10:33.515 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 5
  2020-09-22 15:10:33.534 26627-26627/com.example.viewpager2_example_2 D/frg F: onStart()
  2020-09-22 15:10:33.552 26627-26627/com.example.viewpager2_example_2 D/frg D: onStop()
  2020-09-22 15:10:33.573 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 6
  2020-09-22 15:10:33.587 26627-26627/com.example.viewpager2_example_2 D/frg G: onStart()
  2020-09-22 15:10:33.597 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 7
  2020-09-22 15:10:33.609 26627-26627/com.example.viewpager2_example_2 D/frg H: onStart()
  2020-09-22 15:10:33.633 26627-26627/com.example.viewpager2_example_2 D/frg C: onStop()
  2020-09-22 15:10:33.636 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 8
  2020-09-22 15:10:33.647 26627-26627/com.example.viewpager2_example_2 D/frg I: onStart()
  2020-09-22 15:10:33.752 26627-26627/com.example.viewpager2_example_2 D/frg B: onStop()
  2020-09-22 15:10:33.967 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 9
  2020-09-22 15:10:33.972 26627-26627/com.example.viewpager2_example_2 D/frg A: onPause()
  2020-09-22 15:10:33.973 26627-26627/com.example.viewpager2_example_2 D/frg A: onStop()
  2020-09-22 15:10:34.018 26627-26627/com.example.viewpager2_example_2 D/frg F: onStop()
  2020-09-22 15:10:34.023 26627-26627/com.example.viewpager2_example_2 D/frg I: onResume()
  </code></pre>
  <p><strong>Problematic scenario</strong> (stopping debugger in <code>createFragment()</code> method for 1-2 seconds each time):</p>
  <pre><code>2020-09-22 15:14:26.419 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 5
  2020-09-22 15:14:28.551 26627-26627/com.example.viewpager2_example_2 D/frg F: onStart()
  2020-09-22 15:14:28.577 26627-26627/com.example.viewpager2_example_2 D/frg D: onStop()
  2020-09-22 15:14:28.602 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 6
  2020-09-22 15:14:29.591 26627-26627/com.example.viewpager2_example_2 D/frg G: onStart()
  2020-09-22 15:14:29.615 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 7
  2020-09-22 15:14:30.962 26627-26627/com.example.viewpager2_example_2 D/frg H: onStart()
  2020-09-22 15:14:30.999 26627-26627/com.example.viewpager2_example_2 D/frg C: onStop()
  2020-09-22 15:14:31.003 26627-26627/com.example.viewpager2_example_2 D/frg B: onStop()
  2020-09-22 15:14:31.006 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 8
  2020-09-22 15:14:31.922 26627-26627/com.example.viewpager2_example_2 D/frg I: onStart()
  2020-09-22 15:14:31.956 26627-26627/com.example.viewpager2_example_2 D/frg A: onPause()
  2020-09-22 15:14:31.957 26627-26627/com.example.viewpager2_example_2 D/frg A: onStop()
  2020-09-22 15:14:31.965 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 9
  2020-09-22 15:14:33.210 26627-26627/com.example.viewpager2_example_2 D/frg J: onStart()
  2020-09-22 15:14:33.240 26627-26627/com.example.viewpager2_example_2 D/frg G: onStop()
  2020-09-22 15:14:33.248 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 10
  2020-09-22 15:14:34.247 26627-26627/com.example.viewpager2_example_2 D/frg K: onStart()
  2020-09-22 15:14:34.276 26627-26627/com.example.viewpager2_example_2 D/frg F: onStop()
  2020-09-22 15:14:34.280 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 11
  2020-09-22 15:14:35.286 26627-26627/com.example.viewpager2_example_2 D/frg L: onStart()
  2020-09-22 15:14:35.316 26627-26627/com.example.viewpager2_example_2 D/frg H: onStop()
  2020-09-22 15:14:35.320 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 12
  2020-09-22 15:14:36.407 26627-26627/com.example.viewpager2_example_2 D/frg M: onStart()
  2020-09-22 15:14:36.435 26627-26627/com.example.viewpager2_example_2 D/frg I: onStop()
  2020-09-22 15:14:36.439 26627-26627/com.example.viewpager2_example_2 D/frg: createFragment: 13
  2020-09-22 15:14:37.632 26627-26627/com.example.viewpager2_example_2 D/frg N: onStart()
  2020-09-22 15:14:37.656 26627-26627/com.example.viewpager2_example_2 D/frg J: onStop()
  </code></pre>
  <p>Anyone can explain what sort of sorcery is happening here inside <code>ViewPager2</code>?</p>