title: Switching between two fixed fragments
body: <p>I have this activity where I run an asynctask that gathers a series of data. This data can be represented in several ways, for the sake of simplicity let's say there's two ways. I want the user to be able to switch between these two representations (listview and graphical view).</p>
  
  <p>So I created a FragmentActivity with my ListFragment and my GraphicFragment. </p>
  
  <p>How do you switch between these two so that user can go back and forth?  I tried using replace but then the previous fragment gets destroyed which is not of my interest since its reference has the data passed by my Activity. (i.e. I don't want to recreate the fragment each times the user switches between views).</p>
  
  <p>I also tried hiding and showing but I can't get it to work, one of the fragments is not showing.</p>
  
  <p>Also, when I rotate I guess the fragments get destroyed because I get a nullpointerexception when trying to access them.</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_layout);
  
      pBar = (ProgressBar) findViewById(R.id.analyzingProgress);
  
      // However, if we're being restored from a previous state, then don't
      // create the fragment again (please)
      if (savedInstanceState == null) {
  
          firstFragment = new DirListFragment();
          secondFragment = new GraphViewFragment();
  
                           getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, secondFragment).commit();
  
  
          fragments= new ArrayList&lt;IFragment&gt;();
          fragments.add(firstFragment);
          fragments.add(secondFragment);
      }
  }
  
  
      @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if(item.getItemId() == R.id.listView){
  
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
          /*transaction.hide(secondFragment);         
          transaction.show(firstFragment);*/
          transaction.replace(R.id.fragment_container, firstFragment);
          transaction.addToBackStack(null);
  
          transaction.commit();
      }
      else if(item.getItemId() == R.id.graph){
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          /*transaction.hide(firstFragment);
          transaction.show(secondFragment);*/
          transaction.replace(R.id.fragment_container, secondFragment);
          transaction.addToBackStack(null);
  
          transaction.commit();
      }
      return true;
  }
  </code></pre>
  
  <p>The problem comes when I try to access the fragment fields to update their data:</p>
  
  <pre><code>     for(IMyFragment frag: fragments){
          frag.updateData(data);
      }
  </code></pre>
  
  <p>I get a NullPointerException after rotating the screen. </p>
  
  <p>Also when switching fragmenents, their data is not updated (i.e. I start with list fragment, it's showing the proper data, then switch to graphic, no data is shown, switch back to list, no data again.)</p>