Android FragmentStatePagerAdapter returns blank screen when reloading middle fragments
Asked Answered
P

2

7

I'm trying to make an app with 4 tabs using FragmentStatePagerAdapter and a ViewPager.

When I first run the app and if I select the tabs in order (0 to 3) everything goes fine, then if I try to perform the way back (3 to 0) tab 1 shows a blank screen, if, after that, I select tab 2, then is blank as well, if I go to tab 0 and do the way again tab 2 back to life and some times tab 1 back to live but I haven't find out any sequence...

I know it's a mess...The thing is tab 1 use to fail, tab 2 fail some times and tabs 0 and 3 work fine always. If I exchange the position of the fragments everything is the same, some I think is not related with the fragments code...

There's no errors shown in the logcat, debugging the fragments shows that everything goes in the way it should be...

I've been some days trying to fix it with no luck (obviusly...), I've tried to override getItemPosition() to return POSITION_NONE, I've tried to make my own FragmentStatePagerAdapter like here say http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/ but nothing...And some more things that haven't worked for me.

Some code: Main Class

 public class Main extends FragmentActivity implements ActionBar.TabListener {
        private MyPageAdapter mPagerAdapter;
        private ViewPager mPager;

    @Override
    protected void onCreate(Bundle si) {
        super.onCreate(si);
        setContentView(R.layout.activity_main);
        init();
    }

        private void init() {
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        if (mPagerAdapter == null) {

            mPagerAdapter = new PaginaAdapter(getSupportFragmentManager());

            mPager = (ViewPager) findViewById(R.id.pager);
            mPager.setAdapter(mPagerAdapter);
            mPager.requestTransparentRegion(mPager);


            mPager.setOnPageChangeListener(new viewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

            // Tab creation
            for (int i = 0; i < mPagerAdapter.getCount(); i++) {
                View tabView = getLayoutInflater().inflate(R.layout.tab, null);
                ((ImageView) tabView.findViewById(R.id.tabIcon))
                .setImageDrawable(getResources().getDrawable(mPagerAdapter.getIcon(i)));

                           actionBar.addTab(actionBar.newTab().setCustomView(tabView).setTabListener(this));
            }

        }

    }

        @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

Adapter

public class MyPageAdapter extends FragmentStatePagerAdapter {
     public MyPageAdapter(FragmentManager fm) {
          super(fm);
     }

     @Override
     public Fragment getItem(int position) {

        Fragment fragment = null;

        switch (position) {
        case 0:
            fragment = new InitFragment();
            break;
        case 1:
            fragment = new MusicFragment();
            break;
        case 2:
            fragment = new VideoFragment();
            break;
        case 3:
            fragment = new PicFragment();
            break;
        }

        return fragment;
     }

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

....

}

Thanks.

UPDATE: My fragments are in separated classes and they're not static, is that important? Another thing I've just realized is that it seems to be destroying the fragment after I touch the tab because if I swipe slowly I can see the fragment but suddenly everything disappear

Pfennig answered 27/5, 2014 at 11:58 Comment(1)
how to solved this issue please update.Phyllys
C
0

I would suggest you use FragmentPagerAdapter instead of FragmentStatePagerAdapter. It seems you only want to have 3 Tabs and a Fragment for each, which doesn't sound too memory intensive (as long a s they are not full of Bitmaps, etc). When using FragmentStatePagerAdapter you are responsible for restoring the state of each Fragment when it gets re-loaded, with FragmentPagerAdapter the system persists the View hierarchy, which is what you want.

Capapie answered 27/5, 2014 at 12:7 Comment(0)
G
0

Try this use FragmentPagerAdapter

MyPageAdapter .java

public class MyPageAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

         switch (position) {
    case 0:
        fragment = new InitFragment();
        break;
    case 1:
        fragment = new MusicFragment();
        break;
    case 2:
        fragment = new VideoFragment();
        break;
    case 3:
        fragment = new PicFragment();
        break;
    }


        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 4;
    }

}
Gilstrap answered 27/5, 2014 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.