I am trying to make a 4-tab application using FragmentStatePagerAdapter and ViewPager.
When I first launch the application, and if I select the tabs in order (from 0 to 3), everything will be fine, then if I try to execute the return path (from 3 to 0), tab 1 shows a blank screen, if after which I select tab 2, then also empty if I go to tab 0 and again make tab 2 back and some tabs 1 come back to life, but I did not find any sequence ...
I know this is a mess ... The thing is, use tab 1 to crash, tab 2 doesn't work several times, and tabs 0 and 3 always work fine. If I exchange the position of the fragments, everything is the same, some, I think, are not related to the code of the fragments ...
There are no errors in the logarithm, fragment debugging shows that everything is going as it should be ...
Iβve been trying to fix this for a few days without luck (obviusly ...), I tried to override getItemPosition () to return POSITION_NONE, I 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 a few more things that didn't work for me.
Some codes: 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); } });
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 are not static, is this important? Another thing that I just realized is that it seems to destroy the fragment after touching the tab, because if I scroll slowly, I see the fragment, but suddenly everything disappears.
android android-fragments android-viewpager fragmentstatepageradapter
josecash
source share