ViewPager2 and FragmentStateAdapter not updating on notifyDataSetChanged()

648 views Asked by At

I followed this tutorial to create a Tab layout with ViewPager2 and FragmentStateAdapter. In my adapter I have an ArrayList with data for the fragments which I then pass differently according to the position of the fragment. I am passing the data using the arguments, the same as described in the tutorial.

The data is passed correctly the first time, however then I am updating the data from the DemoCollectionAdapter (or Activity where the Adapter is initialized). After updating the data I am calling notifyDataSetChanged() but the data in the Fragments does not update.

I have also tried re-setting the TextViews' text in my Fragment by overriding the onResume method in my Fragment and this works fine for 5 out of my 7 fragments which is very weird. The array enabledSchedule is set by the arguments in the OnViewCreated method.

@Override
    public void onResume() {
        super.onResume();

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                Log.d("DebugFrag-Frag", "I am the UI thread");
                swSlotOne.setChecked(enabledSchedule[0]);
                swSlotTwo.setChecked(enabledSchedule[1]);
                swSlotThree.setChecked(enabledSchedule[2]);
                swSlotFour.setChecked(enabledSchedule[3]);
            }
        });
    }

Am I updating the fragments' data in an inappropriate manner? The tutorial by Android does not seem to mention how to update the data then.

Note This question is different because it uses ViewPager not ViewPager2 and PagerAdapter not FragmentStateAdapter.

0

There are 0 answers