Android activity restarted at orientation change even with configChanges set

廉价感情. 提交于 2019-12-31 03:37:10

问题


I have an Android library project and the main application that uses this library project. The manifest file of the main application looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.myapp"
    android:installLocation="preferExternal"
    android:versionCode="3"
    android:versionName="1.1" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:screenOrientation="sensor"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name"
            android:name="MyAppActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>
</manifest>

I also override the onConfigurationChanged() method in the main application. Now when I run the application in the Android emulator and change the orientation via Ctrl+F11 the main activity of the application is restarted.

According to all notes I found here and on Google this should not happen when I have set the android:configChanges="orientation|screenSize" parameter in the manifest file ... but it does :(

Any ideas what I am missing here?
Thanks in advance!


回答1:


You need to add keyboard|keyboardHidden to the android:configChanges attribute. Certainly on the emulator (and on certain devices, I think), when you change orientations the keyboard state also changes. This is also needed to prevent double-restarts of your activity when you don't have it handling orientation changes.




回答2:


If you use abs, it is a limitation, and you can't solve it. But you don't use that library, so give this in the activity tag in manifest file

 android:configChanges="keyboard|keyboardHidden|orientation"

remove that

 android:screenOrientation="sensor"


来源:https://stackoverflow.com/questions/13149571/android-activity-restarted-at-orientation-change-even-with-configchanges-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!