title: Android - Lock phone to portrait but allow tablet to be both landscape and portrait
body: <p>I am trying to write an app where I want the phone to be locked to portrait, but allow the tablet version to have both landscape and portrait. </p>
  
  <p>I have created something that almost works based on all the following questions...</p>
  
  <p><a href="https://stackoverflow.com/questions/8180764/how-do-i-lock-screen-orientation-for-phone-but-not-for-tablet-android">How do I lock screen orientation for phone, but not for tablet? (Android)</a>
  <a href="https://stackoverflow.com/questions/9324561/how-to-allow-both-landscape-portrait-for-tablets-only">How to allow both landscape/portrait for tablets only</a>
  <a href="https://stackoverflow.com/questions/7668367/portrait-for-phone-landscape-for-tablet-android-layout">Portrait for phone, landscape for Tablet (Android-Layout)</a>
  <a href="https://stackoverflow.com/questions/5788448/landscape-mode-in-tablet-only">landscape mode in tablet only</a>
  <a href="https://stackoverflow.com/questions/20042481/locking-phone-in-portrait-mode">Locking phone in portrait mode</a></p>
  
  <p>What I've done so far is extend Activity into my own base Activity and to include orientation change code in both onCreate and an overwritten onConfigurationChanged</p>
  
  <pre><code>    @Override
      protected void onCreate(Bundle savedInstanceState) {
          if(!(new DeviceHelper().isTablet(this)))
          {
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          }
          super.onCreate(savedInstanceState);
          setupActionBar(getActivity());
  
  
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          //don't reload the current page when the orientation is changed
          if(!(new DeviceHelper().isTablet(this)))
          {
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          }
          super.onConfigurationChanged(newConfig);
  
      }
  </code></pre>
  
  <p>Everything with this works fine (I overwrote the onConfigurationChanged to avoid the activity being destroyed and recreated which lead to a black screen for a fraction of a second).</p>
  
  <p>My main problem is that if you hold the phone in landscape, then the activity remains in portrait...perfect! However, because the phone is in actual landscape then the layout for landscape is used, which is a layout from the tablet.</p>
  
  <p>My understanding was that when setRequestedOrientation is called, then it should fire onConfigurationChanged instantly, however, the fragment is called first, then the layout inflated incorrectly, before then calling onConfigurationChanged, which means the landscape layout is already set.</p>
  
  <p>I understand that I could possibly set the layout in each of my activities, based on the configuration but I don't really want to do that. </p>
  
  <p>So how can I get the app to use the appropriate layout file from the appropriate res folder, based on the setRequestedOrientation.</p>
  
  <p>Thanks</p>