33

As a test sample, there is an application with 2 activities: MainActivity that launches SecondActivity on button click.
It works fine on Android 4.0.4, but on Android 4.1.2 I encountered unexpected behaviour.

AutoRotation in system settings is off (or is on - it doesn't matter, "behind" option is ignored anyway).
android:screenOrientation="landscape" is set for MainActivity and android:screenOrientation="behind" is set for SecondActivity, which means that SecondActivity must be launched in landscape orientation too.
It's true for Android 4.0.4, but on Android 4.1.2 SecondActivity starts with portrait orientation.

AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.my.example.testbehindorientation.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.my.example.testbehindorientation.SecondActivity"
        android:configChanges="screenSize|orientation"
        android:label="@string/title_activity_second"
        android:screenOrientation="behind" >
    </activity>
</application>

SecondActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    logOrientation("onCreate");
}


@Override
protected void onDestroy() {
    super.onDestroy();
    logOrientation("onDestroy");
}

@Override
protected void onResume() {
    super.onResume();
    logOrientation("onResume");
}

private void logOrientation(String prefix) {
        int requestedOrientation = this.getRequestedOrientation();
        WindowManager lWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        Configuration cfg = getResources().getConfiguration();
        int lRotation = lWindowManager.getDefaultDisplay().getRotation();   
        int orientation = cfg.orientation;
        Log.i(LOG_TAG, prefix + ", requestedOrientation is " + requestedOrientation + ", rotation is " + lRotation + ", orientation is " + orientation);
}

Log output without android:configChanges="screenSize|orientation" line in AndroidManifest.xml for SecondActivity after button click in MainActivity:

onCreate, requestedOrientation is 3, rotation is 1, orientation is 2
onResume, requestedOrientation is 3, rotation is 1, orientation is 2
onDestroy, requestedOrientation is 3, rotation is 0, orientation is 1
onCreate, requestedOrientation is 3, rotation is 0, orientation is 1
onResume, requestedOrientation is 3, rotation is 0, orientation is 1

Log with line android:configChanges="screenSize|orientation" included in AndroidManifest.xml:

onCreate, requestedOrientation is 3, rotation is 1, orientation is 2
onResume, requestedOrientation is 3, rotation is 1, orientation is 2

No activity recreation now, but the result is always the same - SecondActivity is starting with portrait orientation! :( That is, SecondActivity rotates to portrait after onResume for some reason. Why?.

Tested on:

  • Samsung Galaxy S3 with Android 4.0.4 (Ok)
  • Samsung Galaxy S3 with Android 4.1.? (Bug)
  • Emulator with Android 4.1.2 with portrait home screen orientation (Bug)
  • Android 4.0.3 emulator (Ok)
  • Android 4.2 emulator (Bug)
6
  • Do you have onConfigurationChanged handler with a call to super?
    – Stan
    Dec 20, 2012 at 8:52
  • @Stan I can add onConfigurationChanged but it cannot help. It only lets me see the moment after onResume when it's called with new portrait orientation (if line android:configChanges="screenSize|orientation" presents in AndroidManifest.xml)
    – moorka
    Dec 20, 2012 at 10:29
  • 7
    There is an open bug report here Dec 22, 2012 at 13:07
  • does it behave as expected when AutoRotation in system settings is ON ?
    – petey
    Dec 26, 2012 at 16:29
  • @forgivegod No, it doesn't. If auto-rotation is ON, SecondActivity ignores "behind" attribute and rotates with the phone screen.
    – moorka
    Dec 26, 2012 at 16:48

2 Answers 2

0

If you are facing problem changing orientation using manifest you change the orientation in java. Here is complete code for manifest and activities.

Manifest.xml

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.my.example.testbehindorientation.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.my.example.testbehindorientation.SecondActivity"
        android:configChanges="screenSize|orientation"
        android:label="@string/title_activity_second" >
    </activity>
</application>

Second activity in landscape

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);            
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Change according to need this is for landscape
    logOrientation("onCreate");
}


@Override
protected void onDestroy() {
    super.onDestroy();
    logOrientation("onDestroy");
}

@Override
protected void onResume() {
    super.onResume();
    logOrientation("onResume");
}

private void logOrientation(String prefix) {
        int requestedOrientation = this.getRequestedOrientation();
        WindowManager lWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        Configuration cfg = getResources().getConfiguration();
        int lRotation = lWindowManager.getDefaultDisplay().getRotation();   
        int orientation = cfg.orientation;
        Log.i(LOG_TAG, prefix + ", requestedOrientation is " + requestedOrientation + ", rotation is " + lRotation + ", orientation is " + orientation);
}

Add the following code in onCreate() method of MainActivity

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Change according to need this is for landscape
-1

android:targetSdkVersion="16"

Remove this statement in your manifest file, because SDKVersion=16 is only available for v4.0.

2
  • SDKVersion = 16 stands for Android 4.1, by the way. Anyway, removing or keeping this statement cannot help.
    – moorka
    Jan 6, 2013 at 8:54
  • Target sdk version is the version you build with. Its completely allowed for it to be a different version than the device its run on. Jul 16, 2021 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.