41

I'm writing an application that uses the FragmentPagerAdapter. The fragments in the adapter need to be updated according to outside data - but that doesn't happen. I noticed that the fragment classes are only instantiated once, in the getItem function:

@Override
public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
}

Even if I delete the class and use a new one, nothing helps - this method is only called once, the first time that the tab is populated, and then never again. Anyone has an idea why?

Thanks!

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
n00b programmer
  • 2,671
  • 7
  • 41
  • 56

3 Answers3

53

getItem will be called whenever the adapter needs a fragment and the fragment does not exist.

If the fragment already exists in the FragmentManager then there is no need to instantiate it and getItem does not need to be called.

To update an existing fragment you would need to retrieve it from the FragmentManager or the adapter and manipulate it accordingly.

By default, the viewpager will create fragments for the visible page and the one next to it. I.e to start with, fragments in position 1 and 2. When you swipe to page 2, the fragment at position 3 will be created etc

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • is it possible to destroy a fragment? – n00b programmer Oct 12 '13 at 22:03
  • 2
    http://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android – Kuffs Oct 12 '13 at 22:06
  • http://stackoverflow.com/questions/15212309/cannot-remove-fragment-from-viewpager-why – Kuffs Oct 12 '13 at 22:08
  • @n00bprogrammer can you please tell me how you can resolve this problem because i also get the same problem. – bhavesh kaila Jul 29 '14 at 05:21
  • +1 for the info about ViewPager creating fragments for the visible page and the one next to it. I wondered why I kept seeing things in tab 1 firing when only tab 0 was visible. – Chris Smith Sep 07 '14 at 23:54
  • In public Fragment getItem(int position) is position 0 based? @Kuffs – tgkprog Sep 18 '15 at 19:35
  • I think so but not looked at it for a while so could be wrong – Kuffs Sep 18 '15 at 19:37
  • "If the fragment already exists in the FragmentManager" how exactly is this determined? ie. how does it know exactly what it's looking for? – hmac Jan 11 '17 at 14:57
  • From the Docs `"ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter."` https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html – Kuffs Jan 11 '17 at 15:17
19

To be more specific than the answer above (which is correct!), getItem is called by FragmentPagerAdapter's instantiateItem(ViewGroup container, int position) method. Just in case that helps :)

4

Simply use FragmentStatePagerAdapter instead of FragmentPagerAdapter

FragmentStatePagerAdapter destroys the instance of unneeded instance of fragments and instantiates again on-demand basis. On the other hand, FragmentPagerAdapter just detach the Fragment and reattach it. So fragments created in FragmentPagerAdapter never get destroyed. That's why I always prefer FragmentStatePagerAdapter.

Neo
  • 3,546
  • 1
  • 24
  • 31
  • 2
    I know this is very old but ill just add in anyway incase. Fragment pager adapter holds on to the fragments in memory, used if there are only a couple of tabs. Fragment state pager adapter will destroy the fragment instance on every swipe to save on memory – Dave Feb 05 '19 at 07:20