FragmentPagerAdapter not working on fragment's recreation
Asked Answered
A

1

6

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

Ascertain answered 27/3, 2015 at 16:33 Comment(1)
Check when and if the default constructor of your fragments are called (via logging). You are probably running into subtle interaction problems with Android's automatic state saving and ViewPager's own state restore mechanism. Fragment.newInstance() is usually a good hint that somebody forgot about automatic layout restore (because it is never called when android loads your view).Mendes
P
6

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.

Preshrunk answered 1/6, 2015 at 4:41 Comment(1)
help here #31477278Lounge

© 2022 - 2024 — McMap. All rights reserved.