This Android application demonstrates that calling the FragmentStateAdapter.notifyDataSetChanged()
method on
a FragmentStateAdapter
which is associated to
a ViewPager2 does not result
in the ViewPager2 recreating
its Fragment
s.
The Modifiable fragment collections
section in
the Migrate from ViewPager to ViewPager2
page seems to imply that the UI should be updated, as follows:
ViewPager2
supports paging through a modifiable collection of fragments, callingnotifyDatasetChanged()
to update the UI when the underlying collection changes.This means that your app can dynamically modify the fragment collection at runtime, and
ViewPager2
will correctly display the modified collection.
The FragmentStateAdapter
implementation in this application
(i.e. ViewPagerAdapter) is
absolutely minimal such that only the createFragment(position:)
and getItemCount()
methods are overridden.
See
the MainActivity.onCreate(savedInstanceState:)
method for the place where
the FragmentStateAdapter
is associated to the ViewPager2
. See
the MainActivity.onOptionsItemSelected(item:)
method for the place where FragmentStateAdapter.notifyDataSetChanged()
is called.