简体   繁体   中英

FragmentPagerAdapter not working on fragment's recreation

I'm using ViewPager with PagerTabStrip in fragment A and everything works fine. Items are populated. ...Until I replace A with B and then replace again B with A .

fragment xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#222222"
    android:textColor="@color/blue" />

</android.support.v4.view.ViewPager>

Debuggin shows that after second replacement getItem(int position) is never reached in FragmentPagerAdapter . Tabs are empty.

Adapter for tabs :

    private class ChannelsFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = dataChannelsList.size();

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        return dataChannelsList.get(position).getLang();
    }

    @Override
    public Fragment getItem(int position) {

//This switch isnt reached for second replacement. Dunno why
        switch (position){
            case 0:
                return FragmentChannelsUkr.newInstance();
            case 1:
                return FragmentChannelsRus.newInstance();
            case 2:
                return FragmentChannelsPol.newInstance();
            default:
                return null;
        }
    }
}
}

But again if I rotate the phone when tabs are empty they suddenly recreated in normal usual way.

the adapter call in onCreateView() :

   View rootView = inflater.inflate(R.layout.fragment_channels,container, false);
    ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    viewPager.setAdapter(new ChannelsFragmentPagerAdapter(getFragmentManager()));

Any ideas what invokes such behaviour? Appreciate any help with explanation.

Tell if some more code needed

You might be running out of memory or it would turn out to be a huge process reloading your fragments again. Try using FragmentStatePagerAdapter instead of FragmentPagerAdapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM