0

When I change fragment page recreate view in instantiateItem. How can I avoid this?

public class ContactPagerAdapter extends PagerAdapter implements IconTabProvider {

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view =null;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.show_content,null);    
        container.addView(view, 0);
        return view;
    }

}
Vahid Moradi
  • 791
  • 7
  • 16

1 Answers1

0

Extend from a FragmentPagerAdapter instead:

FragmentPagerAdapter

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

public static class ContactPagerAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
       //inflate your fragment here
    }
}

This will keep your views in memory instead of recreating them, so be careful about the amount you deal with.

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217