0

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.

Luke Galea
  • 179
  • 2
  • 15
  • 1
    hey @uke you need to override override fun getItemId(position: Int): Long { return yourItemList[position].id.toLong() } – Deepak Vajpayee May 21 '21 at 18:25
  • Thanks I'll try! – Luke Galea May 21 '21 at 19:57
  • Hi @DeepakVajpayee, in 'yourItemList' you are storing an id for each entry? The array id is generally the same as the position, no? – Luke Galea May 24 '21 at 08:14
  • 1
    It can be any unique property of object. In my case I have unique id for every object. – Deepak Vajpayee May 25 '21 at 09:12
  • 1
    please note also override containsItem like below. override fun containsItem(itemId: Long): Boolean { return yourItemList.find { it.id.toLong() == itemId } != null } – Deepak Vajpayee May 25 '21 at 09:14
  • Ok thanks, in my case I found that when I swipe to fragment 5, fragments 4 and 6 are created as well. Upon debugging I found that when fragment 6 is created, the data overrides that in the array. I temporarily fixed it by setting the following `viewPager2.setOffscreenPageLimit(3);` . This means that the fragments are created only once, which I don't think is detrimental for my case since I have 7 fragments only. Maybe if I override getItemId and containsItem, the data overriding/replacement issue will be fixed. – Luke Galea May 25 '21 at 09:25
  • Sorry didn't get your point "I found that when fragment 6 is created, the data overrides that in the array"? please elaborate this linke. @Luke – Deepak Vajpayee May 26 '21 at 12:43
  • For whatever reason (could be a bug from my end), I have 7 fragments in total and a List of 7 objects corresponding to each fragment. At a point in my code I update all the fragments (and I confirmed and made sure that the List was being updated correctly). Then when I press on or swipe to fragment 5, I noticed that fragments 4 and 6 were being created as well (it's the way viewpager works (the offscreen pages). When fragments 4 and 5 are created, the data in my List is correct. When fragment 6 is created, I noticed that the data in my List for the last three fragments changes. – Luke Galea May 26 '21 at 12:48

0 Answers0