Fragmentpageradapter Getitem Is Not Called

FragmentPagerAdapter getItem is not called

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.

FragmentPagerAdapter getItem is not being triggered

Any workaround to overcome this problem?

I've downloaded your code and the problem appears because you don't handle those Fragments right. Most precisely you use nested Fragments in the ViewPager based Fragment and for that ViewPager you create the adapter like this:

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getFragmentManager());

Instead, you should be using getChildFragmentManager() to bind the nested fragments:

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());

Also, you shouldn't pass data through a constructor to a Fragment as that data will not survive a configuration change and bad things will start to appear. Use a Bundle instead.

FragmentPagerAdapter GetItem is never called?

you return Zero on your getCount() so there will be no Item inside your PagerAdapter:

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

When is FragmentPagerAdapter's getItem called?

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



Related Topics



Leave a reply



Submit