0

I want to have 2 different orientation modes depending on the device - if device screen is Extra Large, the orientation should be LANDSCAPE and if device screen is smaller, the orientation should be PORTRAIT.

I have made the following check:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set orientation for tablets and phones
    if (isXLarge()) {     
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {    
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    setContentView(R.layout.activity_main); 
}

The manifest entry for the activity is:

<activity 
    android:configChanges="keyboardHidden" 
    android:launchMode="singleTop" 
    android:theme="@android:style/Theme.NoTitleBar" 
    android:name=".MyActivity"/>

It works fine on mobile phones, however on the Motorola Xoom tablet with Android 3.2 this does not work correctly.

If we assume that the tablet is physically hold in portrait mode, the following sequence of actions happens:

  1. Activity is created and displayed in Portrait Mode - this is displayed visually for half-second
  2. Activity is destroyed and recreated in Landscape Mode.

The problem is that there is always the middle transition in the orientation in which the tablet is currently held physically and after that the activity is recreated with the desired orientation.

Thank you for any information or advice about the problem and please tell me if you need more details.

The method isXLarge() returns correctly the type of display

luben
  • 2,512
  • 4
  • 30
  • 41

1 Answers1

1

If you are handling orientation changes on your own, then a better way to do so is to declare the android:configChanges="orientation" in the manifest of the activity and override the onConfigurationChanged() method. In your case, you set the orientation in the onCreate() method, but the system will change the orientation if the user physically changes the orientation.

I would suggest that you override the onConfigurationChanged() and implement your code to provide the correct orientation.

Gan
  • 1,349
  • 2
  • 10
  • 27
  • Can you explain more how can that be helpful? I don't want to handle the orientation myself until I absolutely must - I just need fixed LANDSCAPE orientation in the XLarge screen and fixed PORTRAIT orientation on the smaller screens. – luben Sep 11 '12 at 07:19
  • 1
    So if I understand correctly, you want to display your app in landscape mode for XLarge screens inspite of which which orientation it is held physically. In such a case if you don not override the `onConfigurationChanged()` method your activity will be destroyed and re-created every time the physical orientation is changed. This means you have to save you data/states every time this happens. From you question i suspected that this might be the cause as to why its destroyed and recreated. – Gan Sep 11 '12 at 08:01
  • It seems to me that I am not skillful enough to understand what you mean and I cannot use your answer as guidance for fixing the issue. So I'll vote +1 and continue to think for another solution... – luben Sep 11 '12 at 08:59
  • try adding orientation to the configChanges in the manifest like this `android:configChanges="keyboardHidden | orientation"`. This would avoid recreating of activity. – Gan Sep 11 '12 at 09:16