53

I'm trying to make an app that has a ViewPager in a Fragment that is part of a TabHost. Everything works out fine. I have my tabbar, I can switch tabs. When I switch to the tab with the ViewPager, all is shown correctly.

However, as soon as I leave this tab with the ViewPager and return this tab, my content is not shown. If I scroll to the side twice I do see my next image and if I go back two times I also see the images are loaded (probably the offscreenloaded).

See that my TabFragment is being reinstantiated when I return to it but the fragments in the ViewPager aren't.

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    mProjectText = (TextView) getView().findViewById(R.id.projectText);
    mProjectText.setText(mActiveProject.getInspirationText());

    mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());

    mPager = (ViewPager)getView().findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    super.onActivityCreated(savedInstanceState);
}

public class AlbumAdapter extends FragmentStatePagerAdapter {

private ArrayList<ProjectContent> mItems;

public AlbumAdapter(FragmentManager fm, ArrayList<ProjectContent> items) {
    super(fm);
    this.mItems = items;
}

@Override
public Fragment getItem(int position) {
    return AlbumContentFragment.newInstance(mItems.get(position));
}

@Override
public int getCount() {
    return mItems.size();
}

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

The mockup for the app

double-beep
  • 5,031
  • 17
  • 33
  • 41
Daan Olislagers
  • 3,253
  • 2
  • 17
  • 35

3 Answers3

196

I found the problem. It took me two days, but hey, it's fixed.

Instead of using

mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());

You should use

mAdapter = new AlbumAdapter(getChildFragmentManager(), mActiveProject.getInspiration());

So much for 5 characters.

Daan Olislagers
  • 3,253
  • 2
  • 17
  • 35
  • 10
    Man you never know how you saved my ass tonight! – Anik Oct 21 '14 at 15:46
  • 7
    By the way don't bother looking for something like getSupportChildFragmentManager() since getChildFragmentManager() is a method of Fragment and not of Activity... – DominicM Feb 05 '15 at 21:34
  • Perfect, mate, would never have found this without your post. – Seltsam Feb 20 '17 at 13:45
  • It works, but second time if we open fragment, it takes 1 second of lag to display first tab listview. Any solution? – Ritzor Jul 06 '17 at 08:12
  • 1
    Thanks! This is because we're adding a fragment in a fragment right? But if it's in an Activity, it should be `getFragmentManager()`? This actually made my day man thanks – Ralph Dec 10 '18 at 05:53
  • How did *oogle expect developers to be able to know such thing? Awful design. Anyway thank you. – Meow Cat 2012 Feb 28 '19 at 12:02
49

I been searching this for long finally found a solution.Change your page adapter instance FragmentPagerAdapter to FragmentStatePagerAdapter example:

class PageAdapter extends FragmentStatePagerAdapter {
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
rajesh v
  • 521
  • 4
  • 4
  • Than you, after FragmentPagerAdapter to FragmentStatePagerAdapter it is working. It is Simple and Short answer. – Ravikumar11 Apr 02 '16 at 17:14
  • 1
    Thanks. Spent to much time on this problem and your simple change worked. – Ryan Fiorini Jul 23 '16 at 13:25
  • OMG, it resolve me problem, but i found a problem with dynamic height of Fragment in ViewPager, my void OnMeasure can't get height of view when showing at second time. – RaymondLe May 19 '17 at 07:55
  • The correct answer is the other, but this also works. Thank you all. – emmgfx Feb 08 '18 at 11:18
  • While this works, this does change how the Fragments are loaded and kept in memory. If you want the behavior of a FragmentPagerAdapter, the other solution is the right one. – DerSeegler Nov 01 '18 at 16:34
8

you should use getChildFragmentManager() instead of getFragmentManager() and FragmentStatePagerAdapter() instead of FragmentPagerAdapter().Hope it will be useful for someone.

Swati
  • 28,069
  • 4
  • 21
  • 41
Akhtar
  • 91
  • 1
  • 2