ViewPager2 goes to first page when a child is modified

I have a ViewPager2 with a FragmentStateAdapter and an unkonwn number of pages; every Fragment child has a recyclerView inside and a shared viewmodel between all fragments.

I listen to livedata of the shared viewmodel (because all fragments are listening to the same livedata); when the livedata is triggered, I update the recyclerView inside every fragment.

However when the recyclerview update occurs, the ViewPager goes automatically to the first page, ruining totally the user experience.

Is there a way to not go back to the first page when data is modified?

#UPDATE 02/03/2021

My FragmentStateAdapter subclass:

class IngredientsViewPagerAdapter(fragment: Fragment, private val ingredientType: Int) :
FragmentStateAdapter(fragment) {

private var numberOfPages : Int = 0

fun setNumberOfPages(numberOfPages : Int){
    if (this.numberOfPages != numberOfPages) {
        this.numberOfPages = numberOfPages
        notifyDataSetChanged()
    }
}


override fun getItemCount(): Int {
    return numberOfPages
}

override fun createFragment(position: Int): Fragment {
    return CustomDrinkIngredientsPagerFragment.newInstance(position, ingredientType)
}}

The fragment used:

class CustomDrinkIngredientsPagerFragment : Fragment(R.layout.view_pager_standard_drinks),
IngredientsListAdapter.IngredientListAdapterInterface{

private val customDrinkViewModel : CustomDrinkViewModel by activityViewModels()
private lateinit var rvIngredients : RecyclerView
private val ingredientListAdapter = IngredientsListAdapter(emptyList(), this@CustomDrinkIngredientsPagerFragment)
private val position by lazy {
    requireArguments().getInt("position")
}
private val ingredientType by lazy {
    requireArguments().getInt("ingredientType")
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    rvIngredients = view.findViewById(R.id.list_recycler_view)
    rvIngredients.apply {
        layoutManager =
            GridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
        adapter = ingredientListAdapter
    }
    customDrinkViewModel.ingredients.observe(viewLifecycleOwner) {
        ingredientListAdapter.submitList(it[ingredientType]!![position])
        Log.d("ROB", "Observed ingredients iType:${ingredientType} position:${position}")
    }
}

override fun onIngredientSelected(ingredient: Ingredient) {
    customDrinkViewModel.addIngredient(ingredient)
}

companion object {
    @JvmStatic
    fun newInstance( position : Int, ingredientType : Int): CustomDrinkIngredientsPagerFragment{
        val args = Bundle()
        args.putInt("position", position)
        args.putInt("ingredientType", ingredientType)
        val fragment = CustomDrinkIngredientsPagerFragment()
        fragment.arguments = args
        return fragment
    }
}}

Answer

I found the problem: it was the parent ViewPager (of the ViewPager above). I thought the problem was the second ViewPager, instead it was the parent.

The actual structure was : ViewPager -> ViewPager -> Fragment with RecyclerView.

Modifying the second ViewPager child, the first ViewPager is notified that a child is modified and notifyDatasetChanged is triggered automatically(jumping to page 0)