为什么`android:screenOrientation =“后面的“在android 4.1.2中没有效果?

作为一个testing样本,有2个活动的应用程序:MainActivity,通过点击button启动SecondActivity。 它在Android 4.0.4上正常工作,但在Android 4.1.2上遇到意外的行为。

系统设置中的AutoRotation已closures(或已打开 – 无关紧要,无论如何都忽略“后面”选项)。 为MainActivity设置了android:screenOrientation="landscape" ,为SecondActivity设置了android:screenOrientation="behind" ,这意味着SecondActivity也必须在横向上启动。 对于Android 4.0.4而言是如此,但在Android 4.1.2上,SecondActivity从纵向开始。

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); } 

在MainActivity中的button单击后,在AndroidManifest.xml中为SecondActivity输出没有android:configChanges="screenSize|orientation"输出:

 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 

包含在AndroidManifest.xml中的android:configChanges="screenSize|orientation"

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

现在没有活动娱乐,但结果总是相同的 – SecondActivity是从纵向开始的! :(也就是说,SecondActivity出于某种原因在onResume之后旋转到纵向,为什么?

testing:

  • 三星Galaxy S3与Android 4.0.4(好的)
  • 三星Galaxy S3与Android 4.1。? (错误)
  • Android 4.1.2带有纵向主屏幕方向的模拟器(Bug)
  • Android 4.0.3模拟器(Ok)
  • Android 4.2模拟器(Bug)

Solutions Collecting From Web of "为什么`android:screenOrientation =“后面的“在android 4.1.2中没有效果?"