Android activity restarted at orientation change e

2019-02-26 10:47发布

问题:

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"