Georgi Angelov
Georgi Angelov

Reputation: 4388

ViewPager - first fragment always gets re-created when out of view ( onPause -> onStop -> onAttach->...onResume )

So I have implemented a ViewPager with a FragmentStatePagerAdapter and I have 2 fragments. I have called the setOffscreenPageLimit() with arguments 1 and 2 in the hope that the fragments won't be re-created but the 1st one always gets re-created while the second one gets created ones.

The first fragment always goes through these steps, as long as it goes out of view ( slide to right to the other fragment, or hit home button, etc.. )

For some reason the second fragment only gets created once and if I swipe to the first fragment, it behaves as expected - it doesn't get re-created unless I hit the home button or the power button. The behavior I want is that the two fragments are created once for the duration of the app.

My fragment structure is the following:

I have an activity with a FrameLayout. I add my SlidingTabFragment to that FrameLayout. The SlidingTabFragment contains two fragments itself ( 2 Tabs )

The FragmentStatePagerAdapter

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

    /** Constructor of the class */
    public TabsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context;
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {
        switch(arg0){
            /** tab1 is selected */
            case 0:
                Fragment1 frag1 = new Fragment1();
                return frag1;
            /** tab2 is selected */
            case 1:
                Fragment2 frag2 = new Fragment2();
                return frag2;
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return Settings.TAB_TITLES[position];
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return Settings.TAB_TITLES.length;
    }
}

The parent Fragment:

public class SlidingTabFragment extends Fragment {

    ViewPager pager = null;
    TabsPagerAdapter adapter = null;
    SlidingTabLayout tabs = null;

    public static SlidingTabFragment newInstance() {
        SlidingTabFragment fragment = new SlidingTabFragment();
        return fragment;
    }

    public SlidingTabFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        adapter =  new TabsPagerAdapter(getChildFragmentManager(), getActivity());

        pager = (ViewPager) view.findViewById(R.id.pager);
        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(2);

        // Assigning the Sliding Tab Layout View
        tabs = (SlidingTabLayout) view.findViewById(R.id.sliding_layout);
        tabs.setCustomTabView(R.layout.custom_tab_view, 0);
        tabs.setDistributeEvenly(true);

        // Setting indicator color to white
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.White);
            }
        });

        tabs.setViewPager(pager);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sliding_tab, container, false);
    }
}

R.layout.fragment_sliding_tab

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_series_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.myapp.views.SlidingTabLayout
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/tab_bar_background_color"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1" />
</LinearLayout>

Upvotes: 3

Views: 1878

Answers (1)

Sahar Avr
Sahar Avr

Reputation: 131

Add this to your fragment:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
       // fragment is no longer visible
    }
}

Upvotes: 1

Related Questions