title: ViewPager2 crash
body: <p>I am using Advance Navigation Component with BottomNavigationView.</p>
  <p>In <em>one tab</em> I have ViewPager2. When I clicked on the tab for the first time, it worked fine.</p>
  <p>Although the second time, come on that tab application keep crashing. Below is the crash log. How can I fix this?</p>
  <blockquote>
  <p>java.lang.IllegalArgumentException <br />
  at androidx.core.util.Preconditions.checkArgument(Preconditions.java:36) <br />
  at androidx.viewpager2.adapter.FragmentStateAdapter.onAttachedToRecyclerView(FragmentStateAdapter.java:140) <br />
  at androidx.recyclerview.widget.RecyclerView.setAdapterInternal(RecyclerView.java:1206) <br />
  at androidx.recyclerview.widget.RecyclerView.setAdapter(RecyclerView.java:1158) <br />
  at androidx.viewpager2.widget.ViewPager2.setAdapter(ViewPager2.java:460) <br />
  at com.<strong><strong>.ui.home.history.HistoryFragment.setupAdapter(HistoryFragment.kt:25) <br />
  at com.</strong></strong>.ui.home.history.HistoryFragment.viewSetup(HistoryFragment.kt:21) <br />
  at com.****.base.BaseFragment.onViewCreated(BaseFragment.kt:37) <br />
  at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332) <br />
  at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) <br />
  at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356) <br />
  at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434) <br />
  at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497) <br />
  at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625) <br />
  at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577) <br />
  at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722) <br />
  at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346) <br />
  at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188) <br />
  at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) <br />
  at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) <br />
  at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) <br />
  at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) <br />
  at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) <br />
  at android.os.Handler.handleCallback(Handler.java:873) <br />
  at android.os.Handler.dispatchMessage(Handler.java:99) <br />
  at android.os.Looper.loop(Looper.java:193) <br />
  at android.app.ActivityThread.main(ActivityThread.java:6940) <br />
  at java.lang.reflect.Method.invoke(Native Method) <br />
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) <br />
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)</p>
  </blockquote>
  <p>Here is my code for the fragment:</p>
  <pre><code>private val adapter by lazy {
      HistoryPagerAdapter(this)
  }
  
  override fun viewSetup() {
      binding.vpBuySell.adapter = adapter
      TabLayoutMediator(
          binding.tabBuySell,
          binding.vpBuySell,
          TabLayoutMediator.TabConfigurationStrategy { tab: TabLayout.Tab, i: Int -&gt;
             tab.text = when (i) {
                  0 -&gt; getString(R.string.buy)
                  1 -&gt; getString(R.string.sell)
                  else -&gt; getString(R.string.buy)
              }
          })
  }
  </code></pre>
  <p>Here is the UI code:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;@color/blue_122e47&quot;&gt;
  
      &lt;androidx.appcompat.widget.Toolbar
          android:id=&quot;@+id/toolbar&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;?actionBarSize&quot;
          android:background=&quot;@color/blue_06233e&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:titleTextColor=&quot;@color/white&quot;&gt;
  
          &lt;androidx.appcompat.widget.AppCompatTextView
              android:id=&quot;@+id/tvTitle&quot;
              style=&quot;@style/ToolbarTitleTextStyle&quot;
              android:text=&quot;@string/history&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/btnExport&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;@dimen/_24sdp&quot;
              android:layout_gravity=&quot;end&quot;
              android:layout_marginEnd=&quot;@dimen/_8sdp&quot;
              android:fontFamily=&quot;@font/helvetica_neue_medium&quot;
              android:insetLeft=&quot;0dp&quot;
              android:gravity=&quot;center&quot;
              android:background=&quot;@drawable/shape_export_button&quot;
              android:insetTop=&quot;0dp&quot;
              android:insetRight=&quot;0dp&quot;
              android:insetBottom=&quot;0dp&quot;
              android:foreground=&quot;?selectableItemBackground&quot;
              android:paddingBottom=&quot;@dimen/_2sdp&quot;
              android:paddingStart=&quot;@dimen/_8sdp&quot;
              android:paddingEnd=&quot;@dimen/_8sdp&quot;
              android:text=&quot;@string/export&quot;
              android:textAllCaps=&quot;false&quot;
              android:textColor=&quot;@color/white&quot;
              android:textSize=&quot;@dimen/_12ssp&quot; /&gt;
  
      &lt;/androidx.appcompat.widget.Toolbar&gt;
  
  
      &lt;com.google.android.material.tabs.TabLayout
          android:id=&quot;@+id/tabBuySell&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:background=&quot;@color/blue_122e47&quot;
          app:tabIndicatorFullWidth=&quot;false&quot;
          app:tabIndicatorGravity=&quot;bottom&quot;
          app:tabTextAppearance=&quot;@style/HistoryTabTextStyle&quot;
          app:tabTextColor=&quot;@color/gray_697b8b&quot;
          app:tabSelectedTextColor=&quot;@color/white&quot;
          app:tabIndicatorHeight=&quot;@dimen/_2sdp&quot;
          app:tabIndicatorColor=&quot;@color/blue_47cfff&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot;
          app:tabGravity=&quot;start&quot;
          app:tabMode=&quot;scrollable&quot; /&gt;
  
      &lt;androidx.viewpager2.widget.ViewPager2
          android:id=&quot;@+id/vpBuySell&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@id/tabBuySell&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>Here is my adapter code:</p>
  <pre><code>class HistoryPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
  
      override fun getItemCount(): Int {
          return 2
      }
  
      override fun createFragment(position: Int): Fragment {
          return when (position) {
              0 -&gt; HistoryBuyFragment()
              1 -&gt; HistorySellFragment()
              else -&gt; HistoryBuyFragment()
          }
      }
  
  }
  </code></pre>