Android, how to not destroy the activity when I rotate the device?

AndroidAndroid ActivityOrientation

Android Problem Overview


I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again. How to not destroy the activity?

Android Solutions


Solution 1 - Android

For API 12 and below: add

android:configChanges="orientation"

Add "screenSize" if you are targeting API 13 or above because whenever your orientation changes so does your screen size, otherwise new devices will continue to destroy your activity. See Egg's answer below for more information on using "screenSize"

android:configChanges="orientation|screenSize"

to your Activity in AndroidManifest.xml. This way your Activity wont be restarted automatically. See the documentation for more infos

Solution 2 - Android

From the official document flurin said,

> Note: If your application targets API level 13 or higher (as declared > by the minSdkVersion and targetSdkVersion attributes), then you should > also declare the "screenSize" configuration, because it also changes > when a device switches between portrait and landscape orientations.

So if your app targets API level 13 or higher, you should set this config instead:

android:configChanges="orientation|screenSize"

Solution 3 - Android

The right solution is

android:configChanges="orientation|screenSize"

Android documentation:

>The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. 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).*

Solution 4 - Android

I was messing this up for a little bit and then relized that inside the Manifest file I was putting the configChanges on the Application level and not on the Activity Level. Here is what the code looks like when it is correctly working for me.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                  android:configChanges="orientation|screenSize|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity> 
</application> 

Solution 5 - Android

Now that Android supports split screen ("multi-window" in Android parlance), you'll probably want to add screenSize|smallestScreenSize|screenLayout|orientation as well. So to handle rotation and split screen you'll want something like this in android:configChanges

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                  android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity> 
</application>

Solution 6 - Android

Look at this code in Floating Image. It has the most interesting way of handling screen rotation ever. http://code.google.com/p/floatingimage/source/browse/#svn/trunk/floatingimage/src/dk/nindroid/rss/orientation

Solution 7 - Android

write in manifest:

android:configChanges="orientation|screenSize|keyboardHidden"

and override this in activity that solved your problem:

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVasilView Question on Stackoverflow
Solution 1 - Androiduser235064View Answer on Stackoverflow
Solution 2 - AndroideggView Answer on Stackoverflow
Solution 3 - Androider_benjiView Answer on Stackoverflow
Solution 4 - AndroidsuperheronView Answer on Stackoverflow
Solution 5 - AndroidpkoepkeView Answer on Stackoverflow
Solution 6 - AndroidandroidworkzView Answer on Stackoverflow
Solution 7 - AndroidmojtabaView Answer on Stackoverflow