Q:

cannot restore view pager fragment state

i have a view pager with 3 fragments. i use retrofit rest api to populate each of my fragments for the first time. What i would like to achieve is when the user swipes back either in first or third fragment(those 2 fragment are being being destroyed by the view pager) to restore the data(saved in an array list) and not make a rest api call again. What i have done is to save the array list of downloaded data in onSaveInstanceState() and successfully retrieve it when the user swipes back to one of the 2 above fragments only FOR THE 1 FIRST TIME. The problem is when i navigate back to either one of the 2 fragments the specific bundle key where the array list was saved contains null value.
CompletedSurveysFragment(the third fragment):
public class CompletedSurveysFragment extends Fragment implements SAMVCView {

    private static final String debugTag = CompletedSurveysFragment.class.getSimpleName();
    private View view;
    private RecyclerView completedSurveysRcV;
    private SAMVCPresenterImpl SAMVCpresenterImpl;
    private SurveysRcvAdapter surveysRcvAdapter;
    private List<SurveyData> data;

    public CompletedSurveysFragment() {}

    public static CompletedSurveysFragment newInstance() {
        return new CompletedSurveysFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e(debugTag, "onCreateView");
        if ( view == null ) view = inflater.inflate(R.layout.fragment_completedsurveys, container, false);
        completedSurveysRcV = (RecyclerView) view.findViewById(R.id.completedSurveysRcV);
        return view;
    }

    // TODO: 21/6/2016 configure Limit and offset values
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e(debugTag, "onActivityCreated " + savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        completedSurveysRcV.setHasFixedSize(true);
        completedSurveysRcV.setLayoutManager(linearLayoutManager);
        completedSurveysRcV.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(getActivity(), R.drawable.divider)));
        if (savedInstanceState == null) {
            SAMVCpresenterImpl = new SAMVCPresenterImpl(this);
            SAMVCpresenterImpl.getSurveysBasedOnSpecificFirmId(new AllSurveysBody(getResources().getString(R.string.list_surveys), LoginActivity.getSessionPrefs(getActivity()).getInt(getResources().getString(R.string.firm_id), 0), getResources().getString(R.string.completed), 8, 0));

            surveysRcvAdapter = new SurveysRcvAdapter(null, completedSurveysRcV);
            completedSurveysRcV.setAdapter(surveysRcvAdapter);
        } else {
            //Log.e(debugTag, savedInstanceState.+"");
            if (savedInstanceState.getParcelableArrayList("data") != null) {
                Log.e(debugTag, "here "+ savedInstanceState);
                surveysRcvAdapter = new SurveysRcvAdapter(savedInstanceState.<SurveyData>getParcelableArrayList("data"), completedSurveysRcV);
                completedSurveysRcV.setAdapter(surveysRcvAdapter);
                surveysRcvAdapter.notifyDataSetChanged();
            }
            //Log.e(debugTag, getArguments().getParcelableArrayList("data")+"");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) this.data);
        Log.e(debugTag, "CompletedFragment onSaveInstanceState "+ outState);
    }

    @Override
    public void onSuccessSurveysFetched(List<SurveyData> data) {
        this.data = data;
        surveysRcvAdapter = new SurveysRcvAdapter(data, completedSurveysRcV);
        completedSurveysRcV.setAdapter(surveysRcvAdapter);
        surveysRcvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure() {

    }
}

View pager adapter:
public class SurveysPagerAdapter extends FragmentStatePagerAdapter {

    private static final String debugTag = SurveysPagerAdapter.class.getSimpleName();
    private List<SurveyData> data;
    String[] tabText;

    public SurveysPagerAdapter(FragmentManager fragmentManager, String[] tabText) {
        super(fragmentManager);
        this.tabText = tabText;
    }

    @Override
    public Fragment getItem(int position) {
        Log.e("SurveysPagerAdapter", position+"");
        switch (position) {
            case 0:
                return CompletedSurveysFragment.newInstance();
            case 1:
                return OngoingSurveysFragment.newInstance();
            case 2:
                return PendingSurveysFragment.newInstance();
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabText[position];
    }

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

A:

Your issue is that you're always returning a new instance of your fragment. Instead of calling CompletedSurveysFragment.newInstance(); (and your other Fragments) every time user swipes, create an array of fragments and retrieve it this way:
    ...
    Fragment [] pages = new Fragment[getCount()];
    ...
     @Override
        public Fragment getItem(int position) {
            Log.e("SurveysPagerAdapter", position+"");
    switch (position) {
                case 0:
                    if(pages[position] == null)
                        pages[position] =  CompletedSurveysFragment.newInstance();
                    return pages[position];
    ...
    }

Then, you can fetch and cache your data in onCreate() and retrieve it later in onResume() in your respective fragments.

