86

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?

Vasil
  • 1,073
  • 1
  • 11
  • 11
  • 1
    This is often advised against, you could try setting your applications orientation programatically with something like: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); – stealthcopter Jul 25 '10 at 13:52
  • 7
    Why would it be against? Let's say we have a service that takes 2 seconds to run. If the activity destroys every time, that service keeps running every time while I just need it to run once. – Mohamed Mansour Aug 20 '10 at 00:21

7 Answers7

137

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

ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • The activity is restarted again. I don't know what's the point of doing all the stuff that has to be done when the activity rotates, even if it is not rotating virtually. Is there a way to disable that messages that the sensor sends to the OS about the rotation, or something like that, to fix this problem from its root? – Vasil Jul 27 '10 at 00:56
  • 6
    on what device are you programming? remember, that for example sliding out the keyboard could also restart your activity... (if that's the reason set confChanges to "keyboard|keyboardHidden|orientation") –  Jul 27 '10 at 17:29
  • 2
    This disables the layout change. How do I fix that? – Noobification Aug 06 '15 at 19:17
  • 1
    This solution resolves the destroy and creating activity but, how to handle layout changes. I mean to say when the change the orientation, loading corresponding layout. – sandeepmaaram Sep 21 '15 at 07:15
  • Is there a way to set the android:configChanges parameter programmatically, as opposed to in the manifest file? – CodyF Feb 18 '16 at 17:52
110

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"

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
egg
  • 1,236
  • 2
  • 8
  • 4
9

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).*

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
er_benji
  • 305
  • 4
  • 9
6

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> 
AamirR
  • 11,672
  • 4
  • 59
  • 73
superheron
  • 343
  • 1
  • 3
  • 9
  • 2
    You should share code within your post instead of an image. – Jules Dupont Feb 10 '18 at 16:48
  • Please **[edit]** your post and show the actual manifest code as text instead of screenshots. Others can't copy and paste from your images. [See here](https://meta.stackoverflow.com/a/285557/1402846) for details. Thank you. – Pang Mar 05 '18 at 02:53
1

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>
pkoepke
  • 141
  • 3
0

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

androidworkz
  • 2,902
  • 1
  • 19
  • 19
-1

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);
}
mojtaba
  • 86
  • 2
  • 6
    Why would overriding the onConfigurationChanged method and calling the original implementation doing anything useful? – ricosrealm Aug 01 '17 at 01:17