FragmentPagerAdapter not working on rest fragment

I am using ViewPager with PagerTabStrip in fragment A and everything is working fine. Elements are filled. ... until I replace A with B , and then replace B with A again.

xml snippet :

 <?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 the second replacement, getItem(int position) never reached in the FragmentPagerAdapter . Tabs are empty.

Tab Adapter :

  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 turn the phone when the tabs are empty, they suddenly recreate in the usual usual way.

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 that cause this behavior? Appreciate any help with explanation.

Tell me if you need any more code.

+6
source share
1 answer

You may have run out of memory, or it will be a huge process that will reload your fragments again. Try using FragmentStatePagerAdapter instead of FragmentPagerAdapter .

+3
source

Source: https://habr.com/ru/post/984329/


All Articles