答案 0 :(得分:124)
答案 1 :(得分:106)
来自官方文件flurin说,
注意:如果您的应用程序的目标是API等级13或更高(如声明的那样) 通过minSdkVersion和targetSdkVersion属性),那么你应该 还声明了“screenSize”配置,因为它也会发生变化 当设备在纵向和横向之间切换时。
因此,如果您的应用针对的是API级别13或更高级别,则应设置此配置:
android:configChanges="orientation|screenSize"
答案 2 :(得分:9)
正确的解决方案是
机器人:configChanges ="取向|屏幕尺寸"
Android文档:
当前可用的屏幕尺寸已更改。这表示当前可用大小相对于当前宽高比的变化,因此当用户在横向和纵向之间切换时会发生变化。但是,如果您的应用程序的目标是API级别12或更低,那么您的活动始终会自行处理此配置更改(即使在Android 3.2或更高版本的设备上运行,此配置更改也不会重新启动您的活动。)
答案 3 :(得分:4)
我把它搞砸了一点,然后在Manifest文件中重新设置了我将configChanges放在应用程序级别而不是活动级别。以下是代码在为我正确工作时的样子。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
答案 4 :(得分:1)
现在,Android支持拆分屏幕(在Android术语中为“多窗口”),您可能还需要添加screenSize | smallestScreenSize | screenLayout | orientation。因此,要处理旋转和拆分屏幕,您需要在android:configChanges
中添加类似内容<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
答案 5 :(得分:0)
答案 6 :(得分:-1)
写清单:
android:configChanges="orientation|screenSize|keyboardHidden"
并在解决问题的活动中覆盖此内容:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}