作为一个测试样本,有一个包含 2 个活动的应用程序:在按钮单击MainActivity
时启动。
它在 Android 4.0.4 上运行良好,但在 Android 4.1.2 上我遇到了意外行为。SecondActivity
系统设置中的 AutoRotation 已关闭(或已打开 - 没关系,“后面”选项无论如何都会被忽略)。
android:screenOrientation="landscape"
is set forMainActivity
和android:screenOrientation="behind"
is set for SecondActivity
,这意味着也SecondActivity
必须以横向启动。
对于 Android 4.0.4 确实如此,但在 Android 4.1.2SecondActivity
上是纵向的。
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>
第二活动.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);
}
单击按钮后,不带android:configChanges="screenSize|orientation"
行的AndroidManifest.xml
记录输出:SecondActivity
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
日志中包含android:configChanges="screenSize|orientation"
的行AndroidManifest.xml
:
onCreate, requestedOrientation is 3, rotation is 1, orientation is 2
onResume, requestedOrientation is 3, rotation is 1, orientation is 2
现在没有活动娱乐,但结果总是一样的——SecondActivity
从纵向开始!:( 也就是SecondActivity
在onResume之后由于某种原因旋转到纵向。为什么?。
测试:
- 三星 Galaxy S3 与 Android 4.0.4 (好的)
- 三星 Galaxy S3 与 Android 4.1。?(漏洞)
- 带有 Android 4.1.2 的模拟器,带有纵向主屏幕方向(错误)
- Android 4.0.3 模拟器(好的)
- Android 4.2 模拟器(错误)