0

I am using the following simple FragmentStatePagerAdapter and I want to get a reference to one of the fragments, keeping in mind that they may have been destroyed.

public class MyStatePagerAdapter extends FragmentStatePagerAdapter {

    private int count;

    public MyStatePagerAdapter(FragmentManager fm, int count) {
        super(fm);
        this.count= count;
    }

    @Override
    public Fragment getItem(int arg0) {

        switch (arg0) {
            case 0:
                return MyFragment1.newInstance();

            case 1:
                return MyFragment2.newInstance();

            default:
                return null;
        }
    }

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

I saw this question, whose second answer says that if you call FragmentStatePagerAdapter.instatiateItem and there is already a reference to the fragment, it will not call getItem() again. I looked at the source code here and to my understanding this is indeed what happens.

However, I was wondering, can I do something like the following? It seems straightforward enough, but the fact that I haven't seen it being used anywhere makes me suspicious that something is really wrong that I just can't see.

So basically my question is: What would be the errors in using something like the following to access the fragment?

public class MyStatePagerAdapter extends FragmentStatePagerAdapter {

    private int count;
    private MyFragment1 myFragment1;
    private MyFragment2 myFragment2;

    public MyStatePagerAdapter(FragmentManager fm, int count) {
        super(fm);
        this.count= count;
    }

    @Override
    public Fragment getItem(int arg0) {

        switch (arg0) {
            case 0:
                if (myFragment1 == null) {
                    myFragment1 = MyFragment1.newInstance();
                }
                return myFragment1;
            case 1:
               if (myFragment2 == null) {
                   myFragment2 = MyFragment2.newInstance();
               }
               return myFragment2;
            default:
                return null;
        }
    }

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

}

And to get it, I would call getItem(position).

Community
  • 1
  • 1
kenny
  • 1,095
  • 1
  • 12
  • 14

1 Answers1

1

One problem I see with that approach is myFragment1 and myFragment2 not being null. Let's say you get rid of the null check. The problem there would be that you will generate a new Fragment every time you call getItem().

Another problem that I see is that getItem() will always return null. You have no break statements in your switch, so it will go all the way to the default case which is null. If you add the corresponding breaks then the code shouldn't compile until you add corresponding return statements after the if(){...}s.

Also, I would suggest you get into the habit of adding meaningful names to the parameters of your methods.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • Yes, that's why I did the null check modification to getItem(). So if I use it as is (with the null check I mean) are there any other issues that you could see? – kenny May 16 '14 at 15:54
  • You are right about the break, I overlooked it, code updated. However I was wondering if something more fundamental is wrong, memory management-wise. – kenny May 16 '14 at 16:20