2

Here is my code

public static class TestPagerAdapter extends FragmentPagerAdapter {

    Context mContext;

    public TestPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context;
    }

    @Override
    public int getCount() {
        //it is 3
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fm = null;
        switch (position) {
            case 0 :
            case 1 :
            case 2 :
                fm = TestListFragment.getInstance(mContext, position);
                //fm.setRetainInstance(true);
        }

        return fm;
    }
}

Static method in TestFragment to instantiate

public static TestListFragment getInstance(Context context, int position) {

    fragmentInstance = new TestListFragment();
    fragmentInstance.context = context;
    Bundle args = new Bundle();
    args.putInt("num", position);
    fragmentInstance.setArguments(args);
    return fragmentInstance;
}

Problem:

1- There are total three instances of TestListFragment for each position in TestPagerAdapter. everything works fine but when swipe back to 0th or 2nd it calls getitem and done the whole stuff like onCreateView them onActivityCreated is this intended behavior?

2- As FragmentPAgerAdapter does all that checking by itself then why it is still re-instantiating the fragment. Do I need to do this as mentioned here

3- I am using ListFragments all 3 listfragment uses different adapter where is the appropriate location to setAdapter? I am doing it in onActivityCreated.

4- why fm.setRetainInstance(true) is not working? (I am expecting that making it true will not re-instantiate the fragment)

Hope I made my problem clear..

user2095470
  • 356
  • 1
  • 6
  • 23

1 Answers1

2

First thing you need to understand is "How Pager Adapters works":

Suppose you have 3 Item in Pager Adapter:

  • Fist time getItem() will be called for 1 and 2nd Item.
  • when you swipe down to 2nd getItem for 3rd will be called.
  • getItem will not call when you move to 3rd.
  • When you back to 2nd getItem for 1st item will be called.

This is hwo Adapter pattern is working. Hope it helps you to find your answer.

Yashdeep Patel
  • 3,090
  • 1
  • 17
  • 21
  • I understand this and this what it is happening. My issue is I don't want it to happen as it re-instantiate the fragment instance and go through the complete process. refer [http://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state] (this) – user2095470 Mar 12 '14 at 12:51
  • One thing you can do is make TestListFragment tlf1, tlf2, tlf3 as TestPagerAdapter class variable and in getItem create new one only if it is null. So you will have one instance for one type of fragment. – Yashdeep Patel Mar 12 '14 at 13:03
  • I think this will be more of a hack solution because fragmentPagerAdapter does it for you. We need to figure out what is missing in the implementation of fragmentPagerAdapter. – user2095470 Mar 12 '14 at 13:12