Activity Restarts when Orientation changes android?

user2699728 picture user2699728 · Jan 10, 2014 · Viewed 7.7k times · Source

I have a application ,where I am using Landscape and potrait mode. I have used android:configChanges="orientation|keyboardHidden" in my every activity. So When I run this on device 2.3 its completely working fine and the activity is not restarted. But When I open the same application in android 4.0 and above the activity gets restarted whenever a orientation is changed. Here is my xml file.

<?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>

Answer

Akhil Jain picture Akhil Jain · Jan 10, 2014

if your android:targetSdkVersion="12" or less

android:configChanges="orientation|keyboardHidden"

if your android:targetSdkVersion="13" or more

android:configChanges="orientation|keyboardHidden|screenSize"

Quote from 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).

Read more here :Handling Orientation Change