即使设置了configChanges,Android活动也会在方向更改时重新启动 [英] Android activity restarted at orientation change even with configChanges set

查看:121
本文介绍了即使设置了configChanges,Android活动也会在方向更改时重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android库项目和使用该库项目的主应用程序.主应用程序的清单文件如下所示:

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>

我还重写了主应用程序中的onConfigurationChanged()方法.现在,当我在Android模拟器中运行该应用程序并通过Ctrl + F11更改方向时,该应用程序的主要活动将重新启动.

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.

根据我在此处和Google上发现的所有注释,当我在清单文件中设置android:configChanges ="orientation | screenSize"参数时,应该不会发生这种情况,但是它会:(

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!

推荐答案

您需要将keyboard|keyboardHidden添加到android:configChanges属性.当然在模拟器上(我认为在某些设备上),当您更改方向时,键盘状态也会更改.如果您没有让活动重新处理方向变化,也需要执行此操作,以防止再次重新开始活动.

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.

这篇关于即使设置了configChanges,Android活动也会在方向更改时重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆