title: Fragments android orientation loss of fragment
body: <p>I'm having a struggle with fragment loss on orientation change.
  At first my fragments seem to be responding well to orientation changes and nothing is wrong.
  But the exception is when I navigate back to a previously made fragment. Since I'm checking that in my fragmenthandler activity.</p>
  
  <p>I chose to use a big if statement instead of switch cases, since that was more within the grasp of my knowledge. The handling of the if statements works correctly, have been debugging it with log statements and nothing seems to be wrong there. Below is the code for the handling of fragments, pre-used or create a new one.</p>
  
  <pre><code>public void fragmentHandle(Fragment fl, Fragment fr, String tl, String tr) {
      Fragment cFragLeft = manager.findFragmentById(R.id.fragment_left);
      Fragment cFragRight = manager.findFragmentById(R.id.fragment_right);
  
      Fragment checkLeft = manager.findFragmentByTag(tl);
      Fragment checkRight = manager.findFragmentByTag(tr);
  
      FragmentTransaction ft = manager.beginTransaction();
  
      if (cFragLeft != null &amp;&amp; cFragRight != null &amp;&amp; checkLeft != null
              &amp;&amp; checkRight != null) {
          ft.detach(cFragRight).detach(cFragLeft).attach(fr).attach(fl)
                  .addToBackStack(null).commit();
      } else if (cFragLeft != null &amp;&amp; cFragRight != null &amp;&amp; checkLeft == null
              &amp;&amp; checkRight == null) {
          ft.detach(cFragLeft).detach(cFragRight)
                  .add(R.id.fragment_left, fl, tl)
                  .add(R.id.fragment_right, fr, tr).addToBackStack(null)
                  .commit();
      } else if (cFragLeft != null &amp;&amp; cFragRight != null &amp;&amp; checkLeft == null
              &amp;&amp; checkRight != null) {
          ft.detach(cFragLeft).detach(cFragRight).attach(fr)
                  .add(R.id.fragment_left, fl, tl).addToBackStack(null)
                  .commit();
      } else if (cFragLeft != null &amp;&amp; cFragRight != null
              &amp;&amp; checkRight == null &amp;&amp; checkLeft != null) {
          ft.detach(cFragLeft).detach(cFragRight).attach(fl)
                  .add(R.id.fragment_right, fr, tr).addToBackStack(null)
                  .commit();
      } else {
          ft.add(R.id.fragment_left, fl, tl).add(R.id.fragment_right, fr, tr)
                  .commit();
      }
  }
  </code></pre>
  
  <p>When I change orientation, and then navigating back to a previously saved fragment (first if statement) both of my left and right fragments show blank screens. This happens only when case 1 is called, it doesn't seem to remember the fragments that were there, even though the fragmentmanager found them by tag.</p>
  
  <p>I've also been checking my onCreate, and it's shown below.</p>
  
  <pre><code>    protected void onCreate(Bundle savedInstanceState) {
      // super.onCreate
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_fragment_handler);
  
      if (savedInstanceState == null) {
          fragmentHandle(nFragLeft, nFragRight, "menuMain", "main");
      }
  
  }
  </code></pre>
  
  <p>Also including a button statement just for completion's sake. Haven't been using onClickListeners and not really sure whether I should prefer this over xml onClick or not.</p>
  
  <pre><code>    public void sendDestination(View v) {
      nFragLeft = menudestFrag;
      nFragRight = destFrag;
      String tagLeft = "menuDestination";
      String tagRight = "destination";
  
      fragmentHandle(nFragLeft, nFragRight, tagLeft, tagRight);
  }
  </code></pre>
  
  <p>Is there any way to work around this? Only happens after changing orientation.</p>