title: Using multiple layouts breaks orientation change
body: <p>I've succeeded in handling configuration change with fragments, but I only used one XML layout for the container.</p>
  
  <p>Now I need to use a layout for landscape mode, and when I turn my phone and try to change currently displayed fragments I get an error:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
  at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1328)
  at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1346)
  at android.app.BackStackRecord.commitInternal(BackStackRecord.java:729)
  at android.app.BackStackRecord.commit(BackStackRecord.java:705)
  </code></pre>
  
  <p>Here are my two layouts:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_layout_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" /&gt;
  </code></pre>
  
  <p>And for the landscape one:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_layout_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" /&gt;
  </code></pre>
  
  <p>As you can see both are the exact same except for the orientation.
  I guess the problem might come from reusing old fragments which were added using another orientation ?</p>
  
  <p>Thanks for your help</p>
  
  <p>Edit:
  Code for my activity</p>
  
  <pre><code>package crysteo.pluggicontroller;
  
  public class MainFragmentActivity extends FragmentActivity implements StateModifier,IpAddressChangedListener {
  
  private RemoteFragment remoteFragment;
  private MainFragment mainFragment;
  private DeviceListFragment deviceListFragment;
  private RetainedFragment retainedFragment;
  private HandleConnection handleConnection;
  private ConnectedFragment connectedFragment;
  private CameraFragment cameraFragment;
  private final PluggiHandler handler = new PluggiHandler(this);
  
  
  private static class PluggiHandler extends Handler {
      private final WeakReference&lt;MainFragmentActivity&gt; mainFragmentActivityWeakReference;
  
      public PluggiHandler(MainFragmentActivity mainFragmentActivity) {
          this.mainFragmentActivityWeakReference = new WeakReference&lt;&gt;(mainFragmentActivity);
      }
  
      @Override
      public void handleMessage(Message msg) {
          MainFragmentActivity mainFragmentActivity = this.mainFragmentActivityWeakReference.get();
          BluetoothConstants.BluetoothMessageWhat msgEnum = BluetoothConstants.BluetoothMessageWhat.values()[msg.what];
          switch (msgEnum) {
              case MESSAGE_STATE_CHANGE:
                  BluetoothConstants.BluetoothStates argEnum = BluetoothConstants.BluetoothStates.values()[msg.arg1];
                  switch (argEnum) {
                      case STATE_CONNECTED:
                          mainFragmentActivity.connected();
                          break;
                      case STATE_CONNECTING:
                          Toast.makeText(mainFragmentActivity.getApplicationContext(), R.string.connecting_bluetooth, Toast.LENGTH_SHORT).show();
                          break;
                      case STATE_NONE:
                          break;
                      default:
                          break;
                  }
                  break;
              case MESSAGE_WRITE:
                  /*byte[] writeBuf = (byte[]) msg.obj;
                  // construct a string from the buffer
                  String writeMessage = new String(writeBuf);
                  mConversationArrayAdapter.add("Me:  " + writeMessage);*/
                  break;
              case MESSAGE_READ:
                  /*byte[] readBuf = (byte[]) msg.obj;
                  // construct a string from the valid bytes in the buffer
                  String readMessage = new String(readBuf, 0, msg.arg1);
                  mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + readMessage);*/
                  break;
              case MESSAGE_DEVICE_NAME:
                  Toast.makeText(mainFragmentActivity, String.format(mainFragmentActivity.getResources().getString(R.string.connected_bluetooth),
                          msg.getData().getString(Constants.DEVICE_NAME)), Toast.LENGTH_SHORT).show();
                  break;
              case MESSAGE_TOAST:
                  if (null != mainFragmentActivity) {
                      Toast.makeText(mainFragmentActivity, msg.getData().getString(Constants.TOAST),
                              Toast.LENGTH_SHORT).show();
                  }
                  break;
              default:
                  break;
          }
          mainFragmentActivity.connected();
      }
  }
  
  ;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_layout);
  
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  
      if (savedInstanceState == null) {
          retainedFragment = new RetainedFragment();
          fragmentTransaction.add(retainedFragment, RetainedFragment.class.toString());
          handleConnection = new HandleConnection(this, handler);
          retainedFragment.setHandleConnection(handleConnection);
  
          deviceListFragment = new DeviceListFragment();
          remoteFragment = new RemoteFragment();
          mainFragment = new MainFragment();
          connectedFragment = new ConnectedFragment();
          cameraFragment = new CameraFragment();
          fragmentTransaction.add(R.id.main_layout_container, mainFragment, MainFragment.class.toString());
      } else {
          mainFragment = (MainFragment) getFragmentManager().findFragmentByTag(MainFragment.class.toString());
          deviceListFragment = (DeviceListFragment) getFragmentManager().findFragmentByTag(DeviceListFragment.class.toString());
          remoteFragment = (RemoteFragment) getFragmentManager().findFragmentByTag(RemoteFragment.class.toString());
          retainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag(RetainedFragment.class.toString());
          connectedFragment = (ConnectedFragment) getFragmentManager().findFragmentByTag(ConnectedFragment.class.toString());
          cameraFragment = (CameraFragment) getFragmentManager().findFragmentByTag(CameraFragment.class.toString());
  
          if (mainFragment == null)
              mainFragment = new MainFragment();
  
          if (deviceListFragment == null)
              deviceListFragment = new DeviceListFragment();
  
          if (remoteFragment == null)
              remoteFragment = new RemoteFragment();
  
          if (connectedFragment == null)
              connectedFragment = new ConnectedFragment();
  
          if (cameraFragment == null)
              cameraFragment = new CameraFragment();
  
          handleConnection = retainedFragment.getHandleConnection();
      }
  
      fragmentTransaction.commit();
  
      mainFragment.setBluetoothListener(handleConnection);
      if (retainedFragment.getSelectedMac() != null)
          mainFragment.onMacAddressChanged(retainedFragment.getSelectedMac());
      remoteFragment.setHandleConnection(handleConnection);
      mainFragment.setStateModifier(this);
      deviceListFragment.setStateModifier(this);
      deviceListFragment.setMacAddressChangedListener(mainFragment);
      connectedFragment.setStateModifier(this);
      connectedFragment.setIpAddressChangedListener(this);
  }
  
  @Override
  public void onDestroy() {
      retainedFragment.setHandleConnection(handleConnection);
      super.onDestroy();
  }
  
  @Override
  public void listDevices() {
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.main_layout_container, deviceListFragment, DeviceListFragment.class.toString());
      fragmentTransaction.addToBackStack("listDevices");
      fragmentTransaction.commit();
  }
  
  @Override
  public void deviceSelected(String macAddress) {
      getFragmentManager().popBackStack();
      retainedFragment.setSelectedMac(macAddress);
      mainFragment.onMacAddressChanged(macAddress);
  }
  
  @Override
  public void connected() {
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.main_layout_container, connectedFragment, ConnectedFragment.class.toString());
      fragmentTransaction.addToBackStack("connected");
      fragmentTransaction.commit();
  }
  
  @Override
  public void onBackPressed() {
      if (getFragmentManager().getBackStackEntryCount() &gt; 0) {
          getFragmentManager().popBackStack();
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public void remoteControl() {
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.remove(connectedFragment);
      fragmentTransaction.add(R.id.main_layout_container, cameraFragment, CameraFragment.class.toString());
      fragmentTransaction.add(R.id.main_layout_container, remoteFragment, RemoteFragment.class.toString());
      fragmentTransaction.addToBackStack("manualMode");
      fragmentTransaction.commit();
  }
  
  @Override
  public void soundsControl() {
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.main_layout_container, cameraFragment, CameraFragment.class.toString());
      fragmentTransaction.addToBackStack("soundControl");
      fragmentTransaction.commit();
  }
  
  @Override
  public void infoDisplay() {
  
  }
  
  @Override
  public void onIpChangedListener(String ip) {
      cameraFragment.setIp(ip);
  }
  }
  </code></pre>
  
  <p>And for the fragment which should be displayed:</p>
  
  <pre><code>public class ConnectedFragment extends Fragment {
  
  private StateModifier stateModifier;
  private IpAddressChangedListener ipAddressChangedListener;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View root = inflater.inflate(R.layout.connected_fragment_layout, container, false);
  
      root.findViewById(R.id.remote_control_button).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              stateModifier.remoteControl();
          }
      });
  
      root.findViewById(R.id.sounds_button).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              stateModifier.soundsControl();
          }
      });
  
      final EditText editText = (EditText)root.findViewById(R.id.ip_edit_text);
  
      root.findViewById(R.id.change_ip_button).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ipAddressChangedListener.onIpChangedListener(editText.getText().toString());
          }
      });
  
      return root;
  }
  
  public void setStateModifier(StateModifier stateModifier) {
      this.stateModifier = stateModifier;
  }
  
  public void setIpAddressChangedListener(IpAddressChangedListener ipAddressChangedListener) {
      this.ipAddressChangedListener = ipAddressChangedListener;
  }
  }
  </code></pre>