title: How to handle multiple Fragments for a single Activity
body: <p>I have recently started to take a look at developing apps for my android device. What started this interest for me is I was playing around with a few arduinos had the great idea to make them communicate with my phone, as say an interface for whatever values I am measuring on the arduino itself. Now I could take the easy way out and use a public source to accomplish this but there isn't as much to learn that way, and I would like it the way I want it. </p>
  
  <p>Now I suppose the first question I need to ask is, would multiple fragments/single activity be the best way for me to accomplish this? Basically, I want 1 connection to the arduino, pull all the values, but depending on the "tab" I have selected I want certain values displayed certain ways. I decided to make each tab a different fragment and just display the values different ways.  Like I said, I am just beginning android development experience so don't have much to base this choice off of. </p>
  
  <p>So being fixated on this multiple fragment idea I have:</p>
  
  <ol>
  <li>Created multiple Fragment.xml files</li>
  <li>Defined a class for each separate view</li>
  <li>Created a List to display available Fragments</li>
  <li>Instantiated and displayed fragment when selected</li>
  </ol>
  
  <p>So essentially my onMenuItemSelect looked like this.</p>
  
  <pre><code>FragmentTransaction FT = getFragmentManager.beginTransaction();
  switch(position){
      case 1:
          FT.replace(R.id.fragment_container, new MyFragment()).commit();
          break;
      case 2:
          FT.replace(R.id.fragment_container, new MySecondFragment()).commit();
          break;
  }
  </code></pre>
  
  <p>The above code worked, it did what I wanted it to without any issues. I don't really like this though, because for each and every Fragment I wanted to add I would need to add a new case to the switch. Also this instantiates a new fragment every time, even if one was already created. Is that a problem? </p>
  
  <p>The biggest problem I had with it is that it isn't the easiest to scale. For 2-3 fragments this isn't the worst way to handle it (in my eyes). I want to be able to have as many fragments I want without an individual case for each one in the switch. So what I did was created a fragmentList to hold a single instance of each of my fragment.</p>
  
  <pre><code>List&lt;Fragment&gt; fragmentList;
  
  private void populateFragmentList();{
      fragmentList = new ArrayList&lt;Fragment&gt;();
      fragmentList.add(new HomeFrag());
      fragmentList.add(new BluetoothFragment());
      fragmentList.add(new USBFragment());
      fragmentList.add(new RCInfoFragment());
      fragmentList.add(new ControllerFragment());
      fragmentList.add(new FingerCordsFrag());
  }
  public void onMenuItemSelect(int position, int curPosition){
      if(fragmentList.get(position).isAdded()){
         getFragmentManager().beginTransaction().show(fragmentList.get(postition))
          .hide(fragmentList.get(curPosition)).commit();
      }
      else
          getFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentList.get(position)).show(fragmentList.get(position)).hide(fragmentList.get(curPosition)).commit();
  }
  </code></pre>
  
  <p>And this method also worked. I could get it to display all of my fragments, without have to re-instantiate each fragment each time. I believe this does what I am wanting it to do, it scales fairly well(better than a switch/case IMO). The problem that I have now is that it all goes crazy when I change orientation. Up until now I was only testing portrait mode, I am not able to view any of my fragments when I select them in other orientation. I can start it in either orientation, and it works, but when I change it during run-time, I am only able to see the one fragment I had open when I changed orientation.</p>
  
  <p>Now, each fragments "onCreateView" is being called, it is just that the display isn't being shown. I believe I have it narrowed down to it isn't being attach to the new activity created from the orientation change. Is There anyway I can reattach fragments that are already created to a new activity. </p>
  
  <p>In summary, the questions I have are:</p>
  
  <ol>
  <li>Is this model even a good way for my application?</li>
  <li>Is there a decent way to handle Fragments that scales well? Can't
  seem to find any examples.</li>
  <li>Is using a ''new MyFragment()'' each time I open a different tab a
  reasonable way to achieve this?</li>
  <li>Is my way of storing my Fragments in a list a reasonable way to
  handle them?</li>
  <li>How do I reattach a fragment to the new Activity after an
  orientation change?</li>
  </ol>
  
  <p>Thank you for your time.</p>
  
  <p>*Had to type all this code on the fly because I, for some reason, couldn't get my C/P'd code to format correctly. </p>