Skip to content

Latest commit

 

History

History

app3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

FragmentStateAdapter.notifyDataSetChanged() alternative workaround

The app1 module in this project demonstrates that the default implementation of the getItemId(position:) and containsItem(itemId:) methods in the FragmentStateAdapter class result in the FragmentStateAdapter.notifyDataSetChanged() call being a no-op. Specifically, the Fragments in the FragmentStateAdapter remaining unchanged when the FragmentStateAdapter.notifyDataSetChanged() method is called. The app2 module in this project demonstrates the proper workaround for this issue. This Android application demonstrates an alternative workaround wherein the Fragments in the FragmentStateAdapter are not recreated but instead receive a method invocation to update themselves.

See the ViewPagerAdapter class for the place where the FragmentStateAdapter.notifyDataSetChanged() call is picked up and passed on to the Fragments. See the MainActivity.onCreate(savedInstanceState:) method for the place where the ViewPagerAdapter is associated to the ViewPager2. See the MainActivity.onOptionsItemSelected(item:) method for the place where FragmentStateAdapter.notifyDataSetChanged() is called.