FragmentPagerAdapter notifyDataSetChanged not working

fragment pager adapter
fragmentstatepageradapter vs fragmentpageradapter
fragmentstatepageradapter remove fragment
viewpager notify dataset changed not working
android refresh fragment pager adapter
fragmentstateadapter
viewpager adapter not working
viewpager2 notifyitemchanged not working

I got a FragmentPagerAdapter. It's getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I call notifyDataSetChanged and yet nothing happened. I DID override the getItemPosition method to return POSITION_NONE:

public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener,
        ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    private PagerTabsFragments fragmentDisplayInfo = null; // for now only a
                                                            // single size
                                                            // back
                                                            // stack

    static class PagerTabsFragments
    {
        public int tabPosition;
        public Fragment fragmentToDisplay;
        public Object fragmentInfo;

        public PagerTabsFragments(int tab, Fragment frag, Object info)
        {
            tabPosition = tab;
            fragmentToDisplay = frag;
            fragmentInfo = info;
        }
    }

    public void SetFragmentToDisplay(int tabPosition, Fragment frag, Object info)
    {
        fragmentDisplayInfo = new PagerTabsFragments(tabPosition, frag, info);
        notifyDataSetChanged();
    }

    public void CancelFragmentToDisplay()
    {
        fragmentDisplayInfo = null;
    }
...
@Override
    public Fragment getItem(final int position)
    {
        if ((fragmentDisplayInfo != null) && (fragmentDisplayInfo.tabPosition == position))
        {
            return fragmentDisplayInfo.fragmentToDisplay;
        }
        final TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public int getItemPosition(Object item)
    {
        if (fragmentDisplayInfo == null)
        {
            return super.getItemPosition(item);
        }
        return POSITION_NONE;
    }
...

And the update from outside is in a click event which sets the fragmentDisplayInfo var. I debuged and saw that the fragmentDisplayInfo is indeed initialized and the getItemPosition method does return POSITION_NONE, but the getItem method is not even called. Why is that?

EDIT:

If you hadn't done so as well, please notice that overiden part of the adapter:

 @Override
public int getItemPosition(Object item)
{
    return POSITION_NONE;
}

I'm not sure about this, but you can try to use FragmentStatePagerAdapter instead of FragmentPagerAdapter . The thing is, i've also run into this issue, and it helped me

notifyDataSetChanged not working · Issue #1 · takaaki7 , Do you mean, you can't change background of view even if calling notifyDataSetChanged and swipe view pager 2, 3 times? Could you show me  FragmentPagerAdapter notifyDataSetChanged not working (3) I have faced the similar problem when I was working on my last project. So I found few Solution. by overriding getItemPosition in the pager adaptor but this is not a good idea.


What Nik Myers is saying is correct. However there is a piece missing. When notifyDataSetChanged is called, the method getItemPosition is called. You need to override this to get the fragments to reload.

 @Override
    public int getItemPosition(Object object) {
        // Causes adapter to reload all Fragments when
        // notifyDataSetChanged is called
        return POSITION_NONE;
    }

FragmentPagerAdapter notifyDataSetChanged not working , I got a FragmentPagerAdapter. It's getItem method can return a fragment according to data it has from the outside. After I update the data its  android - with - viewpager notifydatasetchanged not working ViewPager PagerAdapter not updating the View (20) I'm using the ViewPager from the compatibility library.


I have faced the similar problem when I was working on my last project. So I found few Solution.

  1. by overriding getItemPosition in the pager adaptor but this is not a good idea.

    @Override
    public int getItemPosition(Object object) {
        // it will recreate all Fragments when
        // notifyDataSetChanged is called
        return POSITION_NONE;
    }
    
  2. The second one is cabezas Solution.

  3. Most Stable solution override getItemPosition in below fashion:

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof MyFragment) {
                // Create a new method notifyUpdate() in your fragment 
                // it will get call when you invoke 
                // notifyDatasetChaged();
                ((MyFragment) object).notifyUpdate();
            }
            //don't return POSITION_NONE, avoid fragment recreation.
            return super.getItemPosition(object);
        }
    

Adventures with FragmentStatePagerAdapter - INLOOPX, Also getting notifyDatasetChanged() to work can be… It only detaches the Views from Fragments which are currently not visible. Fragments in the FragmentPagerAdapter are only detached and never removed from the  If notifyDataSetChanged() does not trigger view updates than there is a chance that you have forgotten to call SetLayoutManager() on your RecyclerView (like I did!). Just don’t forget to do this: Java code:


none of the solutions given above works for me. So hope below solution may be helpful for you.

In my code, I have 2 Fragments in viewPager. One is used for 3 EditTexts and 1 button and another fragment has RecyclerView. On button click, my value is going to recyclerView, but it's not updating. So now, I used the below code:

I called the notifyDataSetChanged() on button click. My MainActivity has inner adapter class extends FragmentStatePagerAdapter class. So i called it using mainActivity.mMainAdapter.notifyDataSetChanged(). Hope it helps someone.

FragmentPagerAdapter doesnt reload Tabs : androiddev, When this happens, notifyDataSetChanged() is called. Right now my (Initial) I'​m working on unity, and this helps. Not sure if I'll @AndroidDev: We are excited to tell you more about Android 11, but now is not the time to celebrate. We are  Questions: I have implemented the FragmentPagerAdapter and and using a List<Fragment> to hold all fragments for my ViewPager to display. On addItem() I simply add an instantiated Fragment and then call notifyDataSetChanged(). I am not sure if this is necessary or not. My problem simply… start with fragment 1 [Fragment 1] add new Fragment 2


Every answer here did not work perfectly for me but combining multiple answers into one is right choice.

public final class SomeFragment extends FragmentStatePagerAdapter{
    private final List<Fragment> fragments;
    private Fragment removeFragment; //Fragment to be removed in the next notifydatasetchanged


    @Override
    public int getItemPosition(final Object object) {
       return object.equals(this.removeFragment) ? FragmentStatePagerAdapter.POSITION_NONE : this.fragments.indexOf(object);
    }

}

Return POSITION_NONE only for the fragment you would like to remove from the viewpager. Every other fragment is still in your fragment list. Do not forget to remove the "removeFragment" from the list before calling notifyDataSetChanged();

Updating ViewPager With New Data Dynamically, I got a FragmentPagerAdapter. It's getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I  Proper use of notifyDataSetChanged in your Android application I have seen code written by many people and I must say that many people write weird code when it comes down to something they think doesn't work. Take for instance Android's famous notifyDataSetChanged for an ArrayAdapter or a BaseAdapter; little is known about it to many people.


Migrate from ViewPager to ViewPager2, It is used in conjunction with PagerAdapter, FragmentPagerAdapter or only getItem() and getCount() methods in order to get a working adapter. because it will not gets updated by calling notifyDataSetChanged() method. notifyDataSetChanged not working for Array Adapter For an ArrayAdapter , notifyDataSetChanged only works if you use the add() , insert() , remove() , and clear() on the Adapter. When an ArrayAdapter is constructed, it holds the reference for the List that was passed in.


If you want to use ViewPager2 in your app and are not currently using calling notifyDatasetChanged() to update the UI when the underlying collection changes​. When ViewPager used FragmentPagerAdapter to page through a For example, scrolling would not work for a vertical scroll view inside a  You can view the working sample code here as well. The second approach is to switch the fragment that displays inside the FragmentPagerAdapter by overriding the getItem() method as well as getItemPosition(Object object) which is invoked every time you call viewPager.getAdapter().notifyDataSetChanged(). For example:


The notifyDatasetChanged() method is not meant to be used for refreshing the current displayed Fragments or their views. You need to add some listeners / callbacks to your Fragments if you want