The correct approach would be to override your PagerAdapter
's getItemPosition() to update your PagerAdapter with the new Fragment:
Called when the host view is attempting to determine if an item's position has changed. Returns POSITION_UNCHANGED if the position of the given item has not changed or POSITION_NONE if the item is no longer present in the adapter.
The default implementation assumes that items will never change position and always returns POSITION_UNCHANGED.
In this case, pages that haven't changed return PagerAdapter.POSITION_UNCHANGED
, the Fragment that is removed returns PagerAdapter.POSITION_NONE
, and the new Fragment gets its new position.
You'll change the underlying data in your PagerAdapter
, then call notifyDataSetChanged(). This is what triggers the ViewPager
to call getItemPosition()
for each visible Fragment
, replacing or moving them as needed.