当Orientation改变android时,Activity重启?

参见英文答案 > How do I disable orientation change on Android?                                    12个
我有一个应用程序,我正在使用Landscape和potrait模式.我在我的每个活动中都使用了android:configChanges =“orientation | keyboardHidden”.因此,当我在设备2.3上运行它时,它完全正常工作并且活动没有重新启动.但是当我在android 4.0及更高版本中打开相同的应用程序时,只要方向发生变化,活动就会重新启动.
 这是我的xml文件.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iconnect.collaborator"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <supports-screens android:largeScreens="true"
        android:normalScreens="true" android:smallScreens="true"
        android:anyDensity="true" />
    <supports-screens android:smallScreens="true" />
    <supports-screens android:normalScreens="true" />
    <supports-screens android:largeScreens="true" />


   <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <application

        android:icon="@drawable/cnlogomini"
        android:label="CollaborateNow"
        android:largeHeap="true"
      android:allowBackup="true"
        android:theme="@style/AppTheme" >
        <activity android:name="jim.h.common.android.zxinglib.CaptureActivity"
            android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" />
        <activity android:name=".Register"
          android:configChanges="orientation|keyboardHidden" ></activity>
        <activity android:name=".ppllogincopy"

           android:configChanges="orientation|keyboardHidden"></activity>

<activity android:name=".Password"

           android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".Camerascan"

           android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".TabGroup1Activity"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".newtab"

            android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".SubmitData"

            android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".peoplelist"

         android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".GenerateQrcode"

        android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".contact"

           android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".peoplelistlogin"

          android:configChanges="orientation|keyboardHidden"></activity>
         <activity android:name=".Startnew"
             android:configChanges="orientation|keyboardHidden"

            ></activity>
          <activity android:name=".projectdetails"

           android:configChanges="orientation|keyboardHidden"></activity>
           <activity android:name=".Signup"

            android:configChanges="orientation|keyboardHidden"></activity>
          <activity android:name=".RegisterScan"

            android:configChanges="orientation|keyboardHidden"></activity>
          <activity android:name=".peoplelistscan"

           android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".Profile"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".List"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".Logout"

           android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".scanRegister"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name=".scanList"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name="scanCamera"

            android:configChanges="orientation|keyboardHidden"></activity>
        <activity android:name="com.iconnect.collaborator.Camera"

         android:configChanges="orientation|keyboardHidden"></activity>
       <activity android:name=".collaborations"

           android:configChanges="orientation|keyboardHidden"></activity>


        <activity
            android:name="com.iconnect.collaborator.MainActivity"


            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

解决方法:

如果你的android:targetSdkVersion =“12”或更少

android:configChanges="orientation|keyboardHidden"

如果你的android:targetSdkVersion =“13”或更多

android:configChanges="orientation|keyboardHidden|screenSize"

引用developer.android

Caution: Beginning with Android 3.2 (API level 13), the “screen size”
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the “screenSize” value in addition to the “orientation”
value. That is, you must decalare
android:configChanges=”orientation|screenSize”. However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).

在这里阅读更多:Handling Orientation Change

上一篇:android – 使用onSaveInstanceState保存TextView


下一篇:android – 从MediaStore.Images.Media.DATA获取图像的方向