2

I have 20 FragmentActivity, they all represent like the game screens with different mechanics. I want to put them all in ViewPager. The only thing that comes to mind is this dummy code:

 public static class MyAdapter extends FragmentPagerAdapter {


    @Override
    public Fragment getItem(int position) {
        switch(position){
          case 0:
            return new Fragment1();
          case 1:
            return new Fragment2();
          .........................
          another 20 case statements here
          .........................
          case 20:
            return new Fragment21();
        }
    }

 }

But there should be another way to do it. Will appreciate any help.

3 Answers3

6

FragmentPagerAdapter is smart enough to no instantiate fragments every time they are needed. So you code is OK. That said, 20 fragments kept in memory might be a bit too much. Have a look at FragmentStatePagerAdapter, it will save and restore fragments automatically, without keeping them live in memory all the time.

Instead of using a switch you can have a list of fragments and return from the list:

List<Fragment> fragments;

public Fragment getItem(int pos) {
  return fragments.get(pos);
}

public void addFragment(Fragment f) {
  fragments.add(f);
}
Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • That code just looks ugly. I tried to reconsider what is used in Api demos for lunching Activities, but I failed. –  Aug 30 '11 at 12:01
  • Oh, if that is your concern, just add them to a list. See updated answer. – Nikolay Elenkov Aug 30 '11 at 12:12
  • That looks like moving that lines of the code in another place. I'll call addFragment 20 times inside main Activity. If you say that that's common practice for games with many screens I'll be satisfied:) –  Aug 30 '11 at 12:18
  • If you have to instantiate 20 different classes, you have to do it somewhere. You could use reflection and do it in a loop if you have a list of class names, if that makes you feel better. The point is that you are avoiding branching (switch) by essentially using a lookup table (the list). I don't do games, btw :) – Nikolay Elenkov Aug 30 '11 at 12:35
  • what's a good technique to associate a name/page title to each of the fragments in that list? – topwik Aug 03 '12 at 17:01
1

Use FragmentStatePagerAdapter instead of FragmentPagerAdapter.

FragmentStatePagerAdapter is using ArrayList to store fragments, but FragmentPagerAdapter is using getFragmentByTag.

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
govo
  • 452
  • 6
  • 11
0

I don't think there's anything very wrong with that to be honest, you see that kinda thing in lots of games where you have like a Menu, Game, Pause, Game Over etc screens.

What I would say though is whether you actually need to create a 'new' Fragment each time you change to a different page.

C0deAttack
  • 24,419
  • 18
  • 73
  • 81
  • As I know, FragmentPagerAdapter will create new instance only if I item has not been created yet. As you can see, all Fragments are different classes, so I need that 20 or whatever switch statement and that looked ugly to me. –  Aug 30 '11 at 12:00