Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentPagerAdapter getItem is not being triggered

Currently, with a FragmentActivity, I toggle among 2 type of Fragments using the following code.

private void toggle() {
    Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.content);
    Fragment fragment = null;
    if (oldFragment instanceof ColorFragment) {
        fragment = new ViewPagerFragment();
    } else {
        fragment = new ColorFragment(android.R.color.black);
    }

    getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
}

2 Fragments are being toggle.

  • ColorFragment - A simple fragment which fill up its background with solid black color.
  • ViewPagerFragment - A fragment contains ViewPager. User can swipe between a purple color fragment, and a blue color fragment.

The code which responsible for swiping purple and blue color fragments are as below.

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

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

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new ColorFragment(android.R.color.holo_purple);
        default:
            return new ColorFragment(android.R.color.holo_blue_bright);
        }
    }      
} 

However, I encounter the weird behavior during toggling.

  1. Black color fragment was shown.
  2. Toggling.
  3. View pager, which can swipe between purple and blue fragments shown.
  4. Toggling.
  5. Black color fragment was shown.
  6. Toggling.
  7. Nothing shown, as MyFragmentPagerAdapter's getItem is not being triggered.

I think my situation is similar to FragmentPagerAdapter getItem is not called

However, I prefer not to use FragmentStatePagerAdapter, because of the cost of potentially more overhead when switching between pages.

Any workaround to overcome this problem?

I include a complete workable source code to demonstrate this problem : https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip

like image 801
Cheok Yan Cheng Avatar asked Mar 23 '13 14:03

Cheok Yan Cheng


3 Answers

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.

like image 121
user Avatar answered Nov 02 '22 14:11

user


Global working tested solution.

getSupportFragmentManager() keeps the null reference some times and View pager does not create new fragment instance.Since it finds reference to same fragment. So to over come this use getChildFragmentManager() solves problem in simple way.

Don't

new PagerAdapter(getSupportFragmentManager(), fragments);

Do

new PagerAdapter(getChildFragmentManager() , fragments);

like image 12
Vinayak Avatar answered Nov 02 '22 14:11

Vinayak


Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter

or

you can use new MyFragmentPagerAdapter(this.getChildFragmentManager())

Hope it will help you :)

like image 2
Neo Avatar answered Nov 02 '22 16:11

Neo