FragmentStatePagerAdapter doesn't call getItem on tab reselection

653 Views Asked by At

I have a complex UI setup like this:

Main Fragment -> Fragment with ActionBar.TabListener -> Fragment with FragmentStatePagerAdapter -> ListFragment

This works when the app loads and I go to the Fragment with FragmentStatePagerAdapter the first time, but the issue I have happens when I go to the Fragment with FragmentStatePagerAdapter and ListFragment a second time (so go to another tab and then return to the tab with FragmentStatePagerAdapter). When I select the tab of the FragmentStatePagerAdapter the second time, the getItem method doesn't get called.

I have tried extending both FragmentStatePagerAdapter and FragmentPagerAdapter with the same result. I would rather use FragmentPagerAdapter for usability issues, and because I only have 3 fragments to page through.

Here's some of my code. Please let me know if you need additional code:

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{       
    TrendsLV[] arrayOfTrendsFragments;

    public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
    {

        super(fragmentManager);

        arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
        arrayOfTrendsFragments[0] = new TrendsLV(arrayOfMonths, 0);
        arrayOfTrendsFragments[1] = new TrendsLV(arrayOfMonths, 1);
        arrayOfTrendsFragments[2] = new TrendsLV(arrayOfMonths, 2);
    }

    @Override
    public Fragment getItem(int position) 
    {                       
        return arrayOfTrendsFragments[position];
    }

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

How the code above gets called:

public class TrendsPagerHolder extends Fragment implements ResultsListener
{
private void displayData()
{
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    {   
        if (pagerAdapter == null)
            pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());

        pager.setAdapter(pagerAdapter);
        pager.setCurrentItem(0);
    }
}
}

I also hope this isn't a duplicate. I found other issues with FragmentStatePagerAdapter, but nothing that solved my particular problem.

Thank you.

1

There are 1 best solutions below

1
On

Im pretty sure that returning POSITION_NONE in getItemPosition(Object object) will solve your problems:

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

more info