title: Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?
body: <p>is it possible to switch between Fragments without re-creating them all the time? If so, how?</p>
  
  <p><a href="http://developer.android.com/guide/topics/fundamentals/fragments.html#Transactions" rel="nofollow noreferrer">In the documentation</a> I found an example of how to replace Fragments. </p>
  
  <pre><code>// Create new fragment and transaction
  Fragment newFragment = new ExampleFragment();
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  
  // Replace whatever is in the fragment_container view with this fragment,
  // and add the transaction to the back stack
  transaction.replace(R.id.fragment_container, newFragment);
  transaction.addToBackStack(null);
  
  // Commit the transaction
  transaction.commit();
  </code></pre>
  
  <p>But I don't want to create my Fragments from the scratch every time I need them.</p>
  
  <p>I also found <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentHideShow.html" rel="nofollow noreferrer">this example</a>  of hiding/showing Fragments:</p>
  
  <pre><code>// The content view embeds two fragments; now retrieve them and attach
  // their "hide" button.
  FragmentManager fm = getFragmentManager();
  addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
  addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
  </code></pre>
  
  <p>But how would I create a fragment with an ID outside an XML file? </p>
  
  <p>I think this might be related to <a href="https://stackoverflow.com/questions/5908492/android-honeycomb-get-instance-of-fragments">this question</a>, but there isn't an answer. :/</p>
  
  <p>Thank you very much in advance, 
  jellyfish</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>That's how I'm doing it now:</p>
  
  <pre><code>Fragment shown = fragmentManager.findFragmentByTag(shownFragment);
  
  //...
  
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  if (shown != null) fragmentTransaction.hide(shown);
  
  //switch statetement for menu selection, just one example:
  
  SettingsFragment set = (SettingsFragment) fragmentManager.findFragmentByTag(SET);
  Toast.makeText(this, "Settings:" + set, Toast.LENGTH_LONG).show();
  if (set == null)
  {
          set = new SettingsFragment();
          fragmentTransaction.add(R.id.framelayout_content, set, SET);
  }
  else fragmentTransaction.show(set);
  shownFragment = SET;
  fragmentTransaction.commit();
  </code></pre>
  
  <p>If I call up the settings, then something else, and then go back to settings, the toast gives me "null" first and "Settings:SettingsFragment{40ef..." second.</p>
  
  <p>However, if I replace <code>fragmentTransaction.add(R.id.framelayout_content, set, SET);</code> with <code>fragmentTransaction.replace(R.id.framelayout_content, set, SET);</code> I keep getting "null", "null", "null"... so it doesn't seem to find the Fragment by tag.</p>
  
  <p><strong>Edit2:</strong></p>
  
  <p>Adding <code>fragmentTransaction.addToBackStack(null);</code> did the trick. :)
  This saves the whole hiding/memorizing which fragment is shown part so I suppose it's the most elegant solution for this.</p>
  
  <p>I found <a href="http://programming-android.labs.oreilly.com/ch08.html#ch08_id36032549" rel="nofollow noreferrer">this</a> tutorial quite helpful on the topic.</p>
  
  <p><strong>Edit3:</strong></p>
  
  <p>Looking at my code I realized I could get rid of some parts, so I changed it to:</p>
  
  <pre><code>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  if (shown != null) fragmentTransaction.hide(shown);
  Settings set = (Settings) fragmentManager.findFragmentByTag(SET);
  if (set == null) set = new Settings();
  
  fragmentTransaction.replace(R.id.framelayout_content, set, SET);
  fragmentTransaction.addToBackStack(null);
  fragmentTransaction.commit();
  </code></pre>
  
  <p>However, this invoked an <code>IllegalStateException: Fragment already added</code>, much the same like <a href="https://stackoverflow.com/questions/6250580/fragment-already-added-illegalstateexception">here</a>. Is there an easy way to prevent this? Otherwise I think I might switch back to the hide/show bit.</p>