title: Fragments and Orientation change
body: <p>What is the correct way to handle an orientation change when using Fragments?</p>
  
  <p>I have a landscape layout that contains 2 fragments (instantiated in code into <code>FrameLayout</code>s). When I switch to portrait mode (the layout of which contains only one <code>FrameLayout</code> that holds the left pane only), the right hand fragment is no longer required.</p>
  
  <p>I am receiving an error:</p>
  
  <pre><code>E/AndroidRuntime(4519): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060085 for fragment myFragment{418a2200 #2 id=0x7f060085}
  </code></pre>
  
  <p>which is assume is my activity trying to re-attach the fragment where it was before the orientation change but as the view that contains the fragment does not exist in portrait mode the error is thrown.</p>
  
  <p>I have tried the following hide/remove/detach methods but still get the error. What is the correct way to tell a fragment it is not needed any more and do not try to display?</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public void onCreate(Bundle b) {
      super.onCreate(b);
      Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragholder2);
  
      //rightPane is a framelayout that holds my fragment.
      if (rightPane == null &amp;&amp; f != null) {
           FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
           ft.hide(f);     // This doesnt work
           ft.remove(f);   // neither does this
           ft.detach(f);   // or this
           ft.commit;
      }
  }
  </code></pre>