3

I create the initial content with the data from an arraylist(i get it from firebase) the data changes every 15 minutes, how do i call a recreation or how do i settext from the mainactivity to the fragments / how do i update the textviews in the fragments (any of those would work please)

i've read a looot of people solving this with getItem but remember i have FragmentStateAdapter i don't have those methods viewPager2.getAdapter().notifyDataSetChanged() does nothing

Overridable Methods in FragmentStateAdapter

and then i thougth how about an interface just to change the text in my textviews but it always calls an error about a null pointer because i understand that the fragment is being "paused" while i don't see it because of the cycle and so and so

Calling a recreation of the viewPager2.setAdapter(new PagerAdapter() works but only when the gods want? and i want something to force it or to be relaiable

(Sincerelly i don't know anymore why am i using the newest viewpager2 and PagerAdapter FragmentStateAdapter as Google suggests) should i try my way around viewpager and all those old tools?

  • If a Fragment should be updated, shouldn't *it* be the one listening for changes? – ianhanniballake Sep 26 '20 at 03:51
  • 1
    Did you find a solution for this? – rmdan Feb 15 '21 at 14:34
  • I am experiencing the same issue and all the solutions that I found did not work. Did you manage to fix this? – Luke Galea May 19 '21 at 15:21
  • i feel so ashamed that after all this months i didn't responded to the other guys either. Short answer, i moved the process to the fragments. Imagine that the app opens in color blue, well i put the proces of changing in the Fragment.java instead of the main class – Antonio Obregon May 20 '21 at 16:10

2 Answers2

1

As per Doc we can update the fragment by calling notifyDatasetChanged() therefore by creating a function(setData) in Adapter and inside which calling notifyDatasetChanged() may be work..like below

 fun addData(data: ArrayList<>) {
    mData = data
    notifyDataSetChanged()
    } 
A.Jain
  • 46
  • 2
1

I encountered this problem, my solution is: save the fragment instance using WeakReference in adapter, and invoke the method in fragment when needing to update it's data. The code is like this:

public MyAdapter extends FragmentStateAdapter{
    private Map<Integer, WeakReference<MyFragment>> fragmentMap = new HashMap();

    @Override
    public Fragment createFragment(int position) {
        MyFragment fragment=new MyFragment();
        fragmentMap.put(position, new WeakReference<>(fragment));
        return fragment;
    }

    public void updateData(List<String> dataList){
        for (int position : fragmentMap.keySet()) {
            WeakReference<MyFragment> wr = fragmentMap.get(position);
            if (wr != null && wr.get() != null) {
                MyFragment fragment = wr.get();
                fragment.updateData(dataList.get(position));
            }
        }
    }
}

public MyFragment extends Fragment{
    public void updateData(String data){
        ...
    }
}

Now you can invoke updateData(List dataList) on your adapter to update data, and it is using WeakReference to avoid memory leak in the code.

Adam
  • 51
  • 3