How to avoid re-creating a fragment in ViewPager?

I am using ViewPager with a FragmentStatePagerAdapter that contains 5 pages.

On the first page, I have a ListView with items. When I go to the last page of the ViewPager and I return to the first page, my ListView is empty because Adaptive has been recreated.

Android always calls the following methods: onAttach (), onCreate (), onCreateView (), onViewCreated (), onActivityCreated (), onStart (), and onResume ().

I would not use ViewPager.setOffscreenPageLimit () methods to avoid breaking my pages. Is there an alternative solution?

The behavior I want is the same as Facebook.

What am I doing wrong? How can I store data in my ListView?

Code of my FragmentStatePageAdapter:

public class AdapterMain extends FragmentStatePagerAdapter {
    List<ChicfyFragment> fragments = new ArrayList<ChicfyFragment>();

    public AdapterMain(FragmentManager fm) {
        super(fm);

        fragments.add(new FragmentShop());
        fragments.add(new FragmentSearch());
        fragments.add(new FragmentSell());
        fragments.add(new FragmentNotification());
        fragments.add(new FragmentProfile());
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

The code of the first page of my ViewPager:

public class FragmentShop extends ChicfyFragment implements SwipeRefreshLayout.OnRefreshListener {

    ListView mListView;
    AdapterProductList mAdapter;
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_shop, container, false);

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
        mListView = (ListView) view.findViewById(R.id.fragment_main_shop_listview);

        return view;
    }

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

        // setup adapter
        mAdapter = new AdapterProductList((FragmentActivity) getActivity(), new ArrayList());

        // setup listview
        mListView.setAdapter(mAdapter);
        mListView.setDivider(null);

        // setup swipe
        mSwipeRefreshLayout.setColorScheme(R.color.red, R.color.white, R.color.blue, R.color.white);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        new AsyncTask<Void, Void, Void>() {
            List<String> list = new ArrayList<String>();

            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    Thread.sleep(1000);
                    for(int i=0; i<10; i++) list.add("" + i);
                } catch (Exception e) {}
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);

                mAdapter.clear();
                mAdapter.addAll(list);
                mAdapter.notifyDataSetChanged();

                mSwipeRefreshLayout.setRefreshing(false);
            }
        }.execute();
    }
}
+4
source share
2 answers

I fixed it as follows:

In the onViewCreated () method of my fragment, I avoid re-creating the adapter by checking if it is null.

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

    // setup adapter
    if(mAdapter == null) {
        mAdapter = new AdapterProductList((FragmentActivity) getActivity(), new ArrayList());
        mAdapter.setOnLastItemListener(this);
    }

    ...
    ...

}
+3
source

You can use this line for X fragments of fragments

mViewPager.setOffscreenPageLimit(X);

+5
source

All Articles