我喜欢用户可以指定所需活动的方向:LANDSCAPE或PORTRAIT。
我的活动在AndroidManifest.xml中声明如下:
<activity
android:name=“.ActivityA”
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustResize">
</activity>
在我调用的Activity的onCreate方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int preferedOrientation = /* Code that get the prefered orientation */
setRequestedOrientation(preferedOrientation);
...
}
但是当用户指定LANDSCAPE时,活动首先出现在PORTRAIT中,然后重新创建到LANDSCAPE。
如何避免这种情况?
感谢。
答案 0 :(得分:0)
使用android:configChanges="orientation|screenSize"
代替android:configChanges="keyboardHidden"
<activity
android:name=“.ActivityA”
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize">
</activity>
请参阅Documentation。
警告:从Android 3.2(API级别13)开始,&#34;屏幕大小&#34; 当设备在纵向和横向之间切换时也会发生变化 取向。因此,如果您希望阻止运行时重新启动 开发API级别13或更高级别时的方向更改(如 由minSdkVersion和targetSdkVersion属性声明),你 必须包含&#34;
screenSize
&#34;价值除了&#34;orientation
&#34; 值。也就是说,你必须decalareandroid:configChanges="orientation|screenSize"
。但是,如果你的 应用程序目标API级别12或更低,然后您的活动始终 处理此配置更改本身(此配置更改 即使在Android 3.2或Android上运行,也不会重新启动您的活动 更高的设备)。