129

I am not able to reuse fragment in FragmentPagerAdapter.. Using destroyItem() method, It is deleting the fragment but still does not called getItem() again..There are just 2-3 Images so I am using FragmentPagerAdapter Instead of FragmentStatePagerAdapter..

public class ExamplePagerAdapter extends FragmentPagerAdapter {

    ArrayList < String > urls;
    int size = 0;
    public ExamplePagerAdapter(FragmentManager fm, ArrayList < String > res) {
        super(fm);
        urls = res;
        size = urls.size();
    }

    @Override
    public int getCount() {
        if (urls == null) {
            return 0;
        } else {
            return size;
        }

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = new FloorPlanFragment();
        Bundle b = new Bundle();
        b.putInt("p", position);
        b.putString("image", urls.get(position));
        Log.i("image", "" + urls.get(position));
        fragment.setArguments(b);
        return fragment;
    }
}

And In FragmentActivity,

pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager(), res2)); 
michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
Kanika
  • 10,648
  • 18
  • 61
  • 81
  • 3
    Is there a particular reason you overrode `destroyItem()`? That is not necessary. – CommonsWare Sep 25 '12 at 11:50
  • to initialize again use FragmentStatePagerAdapter also call when you override it super.destroyItem(container, position, object); – faiziii Jan 30 '15 at 20:16

7 Answers7

316

KISS Answer:

Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter.

I got the answer.. Firstly I thought to delete this question as I am doing a very silly mistake but this answer will help someone who is facing the same problem that Instead of FragmentPagerAdapter, use FragmentStatePagerAdapter.

As @BlackHatSamurai mentioned in the comment:

The reason this works is because FragmentStatePagerAdapter destroys as Fragments that aren't being used. FragmentPagerAdapter does not.

Mangesh
  • 5,491
  • 5
  • 48
  • 71
Kanika
  • 10,648
  • 18
  • 61
  • 81
  • 2
    Hate to be another 'me too!' post, but yeah >.< Thanks for not deleting the question. – Paul Ruiz Jul 10 '13 at 16:36
  • My guess would be something along the lines of the fragments getting dumped into a recycling bin for later use when destroyed, so that when a new pager gets created, it gets filled with recycled fragments. Could probably also be fixed by not putting any content specific code in the getItem call (such as setting an image based on the pagers variables, arrays ect...) – Glenn.nz Jul 30 '13 at 22:10
  • 35
    The reason this works is because FragmentStatePagerAdapter destroys as Fragments that aren't being used. FragmentPagerAdapter does not. – BlackHatSamurai Oct 05 '13 at 22:49
  • @Blaine thanks, you cleared my mind. Btw, thank you bro, you saved me from a big problem. – alicanbatur Feb 17 '14 at 12:25
  • 3
    Just for future reference for those who may find this while searching for a specific problem they're having; read up on both FragmentPagerAdapter and FragmentStatePagerAdapter. They behave differently for a reason and your specific use might require one over the other. – Chris Stewart Mar 02 '14 at 03:09
  • 2
    @najibputhawala I used FragmentStatePagerAdapter but still facing same issue. getItem() not called – sam_k Jun 17 '14 at 07:42
  • Thank you for your answer. Actually Everyone did a silly mistake :D – Olkunmustafa Feb 19 '15 at 20:50
  • Thanks for sharing your answer, you saved me a lot of time. +1 – Corbella Aug 03 '15 at 09:42
  • @Kanika what is a kiss answer? – Rohan Bhatia Jun 26 '17 at 21:47
  • Thanks for sharing this answer spent almost 2hrs debugging, trying different things. – Sam Aug 22 '17 at 06:34
  • I have this problme even with you solution. – Mahdi Jun 12 '19 at 15:30
173

Using a FragmentStatePagerAdapter didn't fully fix my problem which was a similar issue where onCreateView was not being called for child fragments in the view pager. I am actually nesting my FragmentPagerAdapter inside of another Fragment therefore the FragmentManager was shared throughout all of them and thus retaining instances of the old fragments. The fix was to instead feed an instance of the getChildFragmentManager to the constructor of the FragmentPagerAdapter in my host fragment. Something like...

FragmentPagerAdapter adapter = new FragmentPagerAdapter(getChildFragmentManager());

The getChildFragmentManager() method is accessible via a fragment and this worked for me because it returns a private FragmentManager for that fragment specifically for situations in which nesting fragments is needed.

  • Keep in mind however to use getChildFragmentManager() your minimum API version must be atleast 17 (4.2), so this may throw a wrench in your gears. Of course, if you are using fragments from the support library v4 you should be okay.
starball
  • 20,030
  • 7
  • 43
  • 238
Jraco11
  • 4,526
  • 3
  • 20
  • 20
  • 25
    This is the correct answer. The problem is due that the fragments nested in other fragments should use getChildrenFragmentManager() instead of getFragmentManager(). – shihpeng Aug 05 '15 at 03:54
  • I same case of nested fragment i still have problem. In my case my last items ( for example 9,10) getItem not called! it is very odd. – Mahdi Jun 12 '19 at 15:32
  • does not work for me because this: adapter = new MyAdapter( ChildFragmentManager ); will cause: Error CS1503 Argument 1: cannot convert from 'Android.App.FragmentManager' to 'Android.Support.V4.App.FragmentManager' – Altivo Aug 27 '20 at 20:13
9

Override long getItemId (int position)

FragmentPagerAdapter caches the fragments it creates using getItem. I was facing the same issue- even after calling notifyDataSetChanged() getItem was not being called.

This is actually a feature and not a bug. You need to override getItemId so that you can correctly reuse your fragments. Since you are removing fragments, your positions are changing. As mentioned in the docs:

long getItemId (int position)

Return a unique identifier for the item at the given position.

The default implementation returns the given position. Subclasses should override this method if the positions of items can change.

Just provide a unique id to each fragment and you're done.

Using a FragementStatePagerAdapter or returning POSITION_NONE in int getItemPosition (Object object) is wrong. You will not get any caching.

Community
  • 1
  • 1
Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
5

I did what @kanika and @Jraco11 had posted but I still had the problem.

So, after a lot of changes, I found one that worked for me and was added to my FragmentPagerAdapter the next code:

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

According to what I read, getItemPosition is used to notify the ViewPager whether or not to refresh an item, and to avoid updates if the items at the visible positions haven't changed.

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
4

method getItem() is used only to create new items. Once they created, this method will not be called. If you need to get item that is currently in use by adapter, use this method:

pagerAdapter.instantiateItem(viewPager, TAB_POS)
B-GangsteR
  • 2,534
  • 22
  • 34
2

There are two different scenarios : 1.) You have same layout for every pager : In that case, it will be better if you'll extend your custom adapter by PagerAdapter and return a single layout.

2.) You have different layout for every pager : In that case, it will be better if you'll extend your custom adapter by FragmentStatePagerAdapter and return different fragmets for every pager.

Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
0

I found that setting a listener on the tab-layout stopped this from being called, probably because they only have space for one listener on tabLayout.setOnTabSelectedListener instead of an array of listeners.

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95