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)
onConfigurationChanged
handler with a call tosuper
?onConfigurationChanged
but it cannot help. It only lets me see the moment afteronResume
when it's called with new portrait orientation (if lineandroid:configChanges="screenSize|orientation"
presents in AndroidManifest.xml)