Skip to content

Latest commit

 

History

History

app1

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

FragmentStateAdapter.notifyDataSetChanged() bug

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 Fragments. 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, calling notifyDatasetChanged() 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.

Links

  • See here for the associated bug report in IssueTracker.
  • See the app2 module in this project for a proper workaround.
  • See the app3 module in this project for an alternative workaround.