I followed the official documentation to setup a TabLayout with ViewPager2. I used the TabLayoutMediator this way to connect the TabLayout with the ViewPager2:
I think this is a bug, its listed on google.
https://issuetracker.google.com/issues/151212195 https://issuetracker.google.com/issues/154751401
There is a solution mentioned in the comments that seems to be working for me. Make your FragmentStateAdapter user the constructor
public FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
@NonNull Lifecycle lifecycle) {
From your container fragment create the adapter as follows:
FragmentManager fm = getChildFragmentManager();
Lifecycle lifecycle = getViewLifecycleOwner().getLifecycle();
fragmentAdapter = new FragmentAdapter(fm, lifecycle);