46

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)
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
moorka
  • 533
  • 4
  • 8
  • Do you have `onConfigurationChanged` handler with a call to `super`? – Stan Dec 20 '12 at 08: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 '12 at 10:29
  • 7
    There is an open bug report [here](http://code.google.com/p/android/issues/detail?id=41730) – Jorge Aguilar Dec 22 '12 at 13:07
  • does it behave as expected when AutoRotation in system settings is **ON** ? – petey Dec 26 '12 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 '12 at 16:48
  • @Irina: did this problem get solved? – TheDevMan Aug 02 '13 at 04:04

2 Answers2

0

android:targetSdkVersion="16"

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

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
V.P.
  • 592
  • 8
  • 20
  • SDKVersion = 16 stands for Android 4.1, by the way. Anyway, removing or keeping this statement cannot help. – moorka Jan 06 '13 at 08: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. – Gabe Sechan Jul 16 '21 at 06:02
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
Programmerabc
  • 329
  • 2
  • 10