13

I am implementing Pager Adapter in a Fragment. When I load the screen First time, it works fine. If i switch to other fragment and goes to previous fragment again the it shows empty screen. If I swipe between different tap and move to first tab again then it shows data.

I think on moving back the tabs which are visible didn't load data but once they are out of view during swipe navigation it loads data.

Here is my Pager Adapter:

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return new Fragment1();
        case 1:
            return new Fragment2();
        case 2:
            return new Fragment3();
        case 3:
            return new Fragment4();
        case 4:
            return new Fragment5();
        case 5:
            return new Fragment6();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 6;
    }
}

I am setting my adapter like this:

viewPager = (ViewPager) rootView.findViewById(R.id.my_pager);
        mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
Gaurav Bharti
  • 1,065
  • 1
  • 14
  • 22
user3789702
  • 201
  • 1
  • 3
  • 13
  • Possible duplicate of [Fragment in ViewPager using FragmentPagerAdapter is blank the second time it is viewed](http://stackoverflow.com/questions/7746652/fragment-in-viewpager-using-fragmentpageradapter-is-blank-the-second-time-it-is) – blahdiblah Jun 16 '16 at 23:41

6 Answers6

42

Just Check if you are using getFragmentManager() to initialize the adapter , Try using getChildFragmentManager() like this :

   mAdapter = new MyPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(mAdapter);
Manish
  • 1,259
  • 3
  • 11
  • 20
  • 6
    I can't believe that this worked! Whyyyy?! These problems are so frustrating in Android. – Rui Nov 19 '15 at 17:40
  • Would also appreciate an explanation. Spent hours trying to trace this down and this fixed it. Guess I need to read up on ChildFragmentManager. – ClayHerendeen Oct 17 '17 at 21:06
  • I think because you initialize it in a nested fragment. – 476rick Jul 02 '18 at 07:09
  • 1
    As in the Doc the definition of getChildFragmentManager() is: getChildFragmentManager() return a private FragmentManager for placing and managing Fragments inside of this Fragment. And the definition of getSupportFragmentManager() is: getSupportFragmentManager() Return the FragmentManager for interacting with fragments associated with this fragment's activity. – Aarun Aug 10 '18 at 06:54
15

Use this,

public class MyPagerAdapter extends android.support.v4.app.FragmentStatePagerAdapter
Hades
  • 3,916
  • 3
  • 34
  • 74
4

I was having the same problem and wasted a whole day on this. So I am posting the solution for my issue so that someone else doesn't have to struggle and waste a lot of time.

My problem was that the Fragments inside viewpager were getting invoked(debugger was getting hit) but I was not able to see it in the view(Even for the 1st time).

Issues were:

  1. The parent of the ViewPager was a Fragment.

I had to use getChildFragmentManager() instead of getFragmentManager().

  1. The parent of the Parent Fragment was a NestedScrollView(The activity in which Fragment was populated).

For some reason, even if we keep the height and width of viewpager as matchparent, it was not getting picked up and was defaulted to 0(Even though it was filled in the preview of the xml). To fix this, we have to add android:fillViewport="true" in your NestedScrollView

Hopefully someone will find this helpfull :)

hushed_voice
  • 3,161
  • 3
  • 34
  • 66
0

Because you are not allowed to have nested fragments.

Fragments within Fragments

Change you class to extends FragmentActivity and it should work

Community
  • 1
  • 1
johng
  • 825
  • 1
  • 9
  • 15
0

My ViewPager is in a Fragment. I solve this problem by Change FragmentPagerAdapter to FragmentStatePagerAdapter,and also use getFragmentManager(). it's works fine;

0

use FragmentStatePagerAdapter instead of FragmentPagerAdapter

and add this line after you set the adapter

viewPager.setOffscreenPageLimit(6);\\ add the number of fragments you have

This will remember fragments in memory and it will never show empty screen. But please keep in mind that this will keep each page's data in memory so use it accordingly.

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40