title: fragments overlaps after activity is recreated
body: <p>In my sample, I try to learn the difference between <code>commitAllowingStateLoss()</code> and <code>commit()</code> methods for fragments. Now I know <code>IllegalArgumentException</code> will appear if using <code>commit()</code> after activity save instance (for example, executed <code>commit()</code> when activity was in background).</p>
  
  <p>But another problem appears, fragments overlap. I don't know why, and cannot solve the problem.</p>
  
  <p>My code is as below.</p>
  
  <p>In FragmentMainActivity there are two buttons for switching fragments. </p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"&gt;
  
      &lt;Button
          android:id="@+id/btn_fragment1"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="fragment 1" /&gt;
  
      &lt;Button
          android:id="@+id/btn_fragment2"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="fragment 2" /&gt;
  &lt;/LinearLayout&gt;
  
  &lt;FrameLayout
      android:id="@+id/root"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffffffff"&gt;&lt;/FrameLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>The framelayout with id "root" is the container for fragments.</p>
  
  <pre><code>public class FragmentMainActivity extends AppCompatActivity implements View.OnClickListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_fragment_main);
  
      findViewById(R.id.btn_fragment1).setOnClickListener(this);
      findViewById(R.id.btn_fragment2).setOnClickListener(this);
  }
  
  PlusOneFragment plusOneFragment;
  PlusTwoFragment plusTwoFragment;
  
  @Override
  public void onClick(View v) {
      int i = v.getId();
      if (i == R.id.btn_fragment1) {
          findViewById(R.id.root).postDelayed(new Runnable() {
              @Override
              public void run() {
                  final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                  if (getSupportFragmentManager().findFragmentByTag("tag1") == null) {
                      plusOneFragment = PlusOneFragment.newInstance("1", "2");
                      transaction.add(R.id.root, plusOneFragment, "tag1");
                  } else {
                      plusOneFragment = (PlusOneFragment) getSupportFragmentManager().findFragmentByTag("tag1");
                  }
                  if (getSupportFragmentManager().findFragmentByTag("tag2") != null) {
                      plusTwoFragment = (PlusTwoFragment) getSupportFragmentManager().findFragmentByTag("tag2");
                      transaction.hide(plusTwoFragment);
                  }
                  transaction.show(plusOneFragment).commitAllowingStateLoss();
              }
          }, 3000);
      } else if (i == R.id.btn_fragment2) {
          findViewById(R.id.root).postDelayed(new Runnable() {
              @Override
              public void run() {
                  final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                  if (getSupportFragmentManager().findFragmentByTag("tag2") == null) {
                      plusTwoFragment = PlusTwoFragment.newInstance("1", "2");
                      transaction.add(R.id.root, plusTwoFragment, "tag2");
                  } else {
                      plusTwoFragment = (PlusTwoFragment) getSupportFragmentManager().findFragmentByTag("tag2");
                  }
                  if (getSupportFragmentManager().findFragmentByTag("tag1") != null) {
                      plusOneFragment = (PlusOneFragment) getSupportFragmentManager().findFragmentByTag("tag1");
                      transaction.hide(plusOneFragment);
                  }
                  transaction.show(plusTwoFragment).commitAllowingStateLoss();
              }
          }, 3000);
      }
  }
  }
  </code></pre>
  
  <p>First I click button1 to show fragment1, then click button2 to show fragment2, it works fine. After those operations, I click button1 again, and then click home, so the app is running in background. To simulated low memorey scene, I then kill the process. All those operations happened in 3s.</p>
  
  <p>Finally, I opened the app from the recent records (activity recreated), found that the two fragments overlapped with each other. How does this happen?(why the problem not happened when the process is foreground?) And what can I do with this problem?</p>
  
  <p><a href="https://i.stack.imgur.com/QYEpv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QYEpv.png" alt="overlapping fragments"></a></p>