5

Okay i'll try and make this as clear as possible. I have a Fragment called CheckerManager which contains a ViewPager. This ViewPager will allow the user to swipe between 3 Fragments all of which are an instance of another Fragment called CheckerFragment. I'm using a FragmentPagerAdapter to handle paging. Here's how it looks

private class PagerAdapter extends FragmentPagerAdapter {
    CharSequence mTabTitles[];

    public PagerAdapter(FragmentManager fm, CharSequence tabTitles[]) {
        super(fm);
        mTabTitles = tabTitles;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_LOTTO);
            case 1:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_DAILY);
            case 2:
                return CheckerFragment.newInstance(MainFragment.DRAW_TITLE_EURO);
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabTitles[position];
    }

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

I know that the ViewPager will always create the Fragment either side of the current Fragment. So say my 3 CheckerFragments are called A, B and C and the current Fragment is A. B has already been created. But my problem is that even though I am still looking at Fragment A, Fragment B is the 'active' Fragment. Every input I make is actually corresponding to Fragment B and not A. The active Fragment is always the one which has been created last by the ViewPager.

I've looked at quite a few things to see if anyone has had the same problem but i'm finding it difficult to even describe what's wrong. I think it's something to with the fact that all of the ViewPagers fragments are of the same type ie - CheckerFragment. I have a working implementation of a ViewPager inside a fragment elsewhere in the application and the only difference I can tell is that each page is a different type of Fragment.

Any help would be appreciated!

*EDIT

PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), tabTitles);
    ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewPagerChecker);
    viewPager.setAdapter(adapter);
Adam Purser
  • 331
  • 2
  • 8
  • Please show the code where you are instantiating `PagerAdapter`. Also, I would encourage you to name that class something other than `PagerAdapter`, as Android has a class named `PagerAdapter`, so you will be forever having to deal with incorrect imports and the like. – CommonsWare Jul 09 '15 at 17:34
  • Thanks for the tip. I've edited my answer to show this – Adam Purser Jul 09 '15 at 18:36
  • Hmmm... that seems OK. Frequently, the problem with nested fragments comes when developers pass `getFragmentManager()`/`getSupportFragmentManager()` instead of `getChildFragmentManager()`. But you have that correct. FWIW, while I have not run this sample in ages, [here is a sample app implemented a `ViewPager` using nested fragments](https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Nested). Perhaps something in there will give you ideas of what is going wrong in your implementation. – CommonsWare Jul 09 '15 at 18:39
  • Okay thanks guys :) will check soon to see if any of these help – Adam Purser Jul 09 '15 at 18:47

1 Answers1

0

I feel pretty stupid but I found out what the issue was. In my CheckerFragment I would call getArguments() to retrieve a String extra and I would use this to determine how to layout the fragment. Problem was I made this extra a static member of CheckerFragment. So every time a new Fragment was created it was using the most recent extra.

Moral of the story - Don't make your fragments extra a static member if you plan on making multiple instances of that fragment.

Adam Purser
  • 331
  • 2
  • 8