title: FragmentTransaction working only in portrait mode
body: <p>I have a really weird behaviour of my Android code.</p>
  
  <p>The actual question is after the code.</p>
  
  <p>What I want to do is having an application which works only in portait mode on phones and in both portrait and landscape mode on tablet. To do so, I created three bool.xml file in different folders. This one is in the folders values:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;bool name="portrait_only"&gt;true&lt;/bool&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>while in the values-xlarge and values-sw600dp I put</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;bool name="portrait_only"&gt;false&lt;/bool&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>The only activity in the project loads, at startup a splash screen by attaching a SplashScreenFragment. Here it is the Activity</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends ActionBarActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      if (getResources().getBoolean(R.bool.portrait_only)) 
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      if (getSupportActionBar() != null)
          getSupportActionBar().hide();
      setContentView(R.layout.activity_main);
  
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
          .add(R.id.container, new SplashScreenFragment()).commit();
      }
  }
  }
  </code></pre>
  
  <p>The fragment itself just loads a layout and starts a TimerTask which changes the fragment. Here it is</p>
  
  <pre class="lang-java prettyprint-override"><code>public class SplashScreenFragment extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_splash_screen, container, false);
  
          Timer splashScreenTimer = new Timer();
          splashScreenTimer.schedule(new SplashScreenTimerTask(getActivity()), 2000);
  
          return rootView;
      }
  
      private class SplashScreenTimerTask extends TimerTask {
          private FragmentActivity activity;
  
          public SplashScreenTimerTask(FragmentActivity activity) {
              this.activity = activity;
          }
  
          @Override
          public void run() {
              FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
              transaction.replace(R.id.container, new FragmentMain());
              transaction.commit();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Now, if I start on my phone, the application in portrait mode, everything works flawlessly, while if I start the app with my mobile in landscape mode, then the first fragment gets loaded perfectly, but when the line </p>
  
  <pre><code>transaction.commit();
  </code></pre>
  
  <p>gets executed, the app crashes with the following error in the logcat</p>
  
  <pre><code>E/AndroidRuntime(17433): FATAL EXCEPTION: Timer-0
  E/AndroidRuntime(17433): Process: it.assottica.android, PID: 17433
  E/AndroidRuntime(17433): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
  E/AndroidRuntime(17433):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1354)
  E/AndroidRuntime(17433):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1372)
  E/AndroidRuntime(17433):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
  E/AndroidRuntime(17433):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
  E/AndroidRuntime(17433):    at it.assottica.android.fragments.SplashScreenFragment$SplashScreenTimerTask.run(SplashScreenFragment.java:40)
  E/AndroidRuntime(17433):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  </code></pre>
  
  <p>Does anybody have any clue why this is happening and how to solve it? But, above all, why everything works fine if I start the app with the mobile in landscape or portrait mode?</p>