70

I am creating an Android app in which I'm drawing a view on a canvas. When the device's orientation changes, the activity restarts. I don't want it to.

How can I avoid restarting the activity when the orientation changes?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
chirag
  • 701
  • 1
  • 5
  • 3

17 Answers17

100

There are various ways to do it, but as given here, using

android:configChanges="keyboardHidden|orientation|screenSize"

allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView.

This is the way I've been doing it, but I'd be interested to know other people's thoughts.

tpetert
  • 39
  • 2
Mike
  • 1,173
  • 1
  • 8
  • 7
  • in my case, i should not use setContentView and onConfigurationChanged should be empty and calling just the super. – M D P Jan 15 '16 at 20:00
22

Define your activity in the AndroidManifest.xml like this:

   <activity
        android:name="com.name.SampleActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:icon="@drawable/sample_icon"
        android:label="@string/sample_title"
        android:screenOrientation="portrait" >
    </activity>
Vikas
  • 4,263
  • 1
  • 34
  • 39
  • 3
    This is misleading. The `android:screenOrientation="portrait"` will enforce a portrait screen orientation at all times. Don't include this line. – Daniel C Jacobs Sep 24 '21 at 15:30
10

Check in your android manifest file that you have written android:configChanges="orientation" on the activity..

Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
ninjasense
  • 13,756
  • 19
  • 75
  • 92
  • 2
    Note that, for the most part, this isn't what you want to do. For most apps, the right approach is to write down your configuration in onSaveInstanceState() and restore it back after the configuration change. – jjb Dec 31 '10 at 06:38
  • thanks.but i am drawing graph on canvas onclick event of button so how can i save state of canvas. – chirag Dec 31 '10 at 07:29
8

For xamarin users,

To avoid the application restart on orientation change in Android, add this

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize

to Activity attribute of all Activity classes. For example, below is my demo code

    [Activity(Label = "DemoApp", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
        //Some code here
        }
    }
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Tushar patel
  • 3,279
  • 3
  • 27
  • 30
  • 1
    at least mention this is Xamarin C# code and not Java – SadeepDarshana Oct 08 '16 at 13:57
  • 1
    Thanks. Very helpful. Just one note: The answer says to add ConfigurationChanges to MainActivity. You should in fact add this to every activity on your project where you want to prevent it from restarting. Not just MainActivity. – Metalogic Nov 29 '16 at 08:20
8

I tried to write android:configChanges="keyboardHidden|orientation|screenSize" in activity tag but in not works.

I tried a lot of methods but nothing works until I added android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities and it works perfectly.

Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19
7

Add android:configChanges="keyboardHidden|orientation" to your activity

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
6

I would recommend using Fragments. You can simply use setRetainInstance(true) to notify that you want to keep your fragment.

Grimmace
  • 4,021
  • 1
  • 31
  • 25
3

Add this to all of your activities in the manifest.

android:configChanges="orientation|screenSize"

Example:

<activity android:name=".activity.ViewActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize"/>
Mohsen mokhtari
  • 2,841
  • 1
  • 30
  • 39
2

TO avoid restart on keyboardHidden|orientation - How to disable orientation change in Android?
Please follow Android API guide - Handling Runtime Changes
Using the Application Class - Activity restart on rotation Android

Community
  • 1
  • 1
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
1

Declare this in your AndroidManifest.xml

<activity android:name=".complex_examples.VideoPlayerActivity"
            android:configChanges="keyboard|keyboardHidden|orientation
                                  |screenSize|screenLayout|smallestScreenSize|uiMode"
            android:launchMode="singleTop"/>

But take care, Android Developers Documentation says that you should do it only if there is no better options left.

Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

If you are sure about doing it, you can handle the configuration changes by your self in onConfigurationChanged() method.

Soon Santos
  • 2,107
  • 22
  • 43
1

To stop from destroying the activity on rotation

`android:configChanges="keyboardHidden|orientation|screenSize"`
Vivek Pratap Singh
  • 1,564
  • 16
  • 26
1

In Android, when the device is rotated, the current activity is destroyed and recreated with the new configuration, which can lead to some unexpected behavior or issues such as data loss, resetting of user input, etc. To prevent this, you can use some techniques to handle the rotation and avoid activity restart.

Disable activity rotation: You can disable the activity rotation by adding android:screenOrientation="portrait" attribute to your activity in the AndroidManifest.xml file. This will force the activity to remain in portrait mode and prevent it from rotating.

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait">

Use onSaveInstanceState: You can use the onSaveInstanceState method to save the state of your activity before it gets destroyed due to rotation. This method is called before the activity is destroyed, and it provides a bundle to store the data.

See more details here.

user16217248
  • 3,119
  • 19
  • 19
  • 37
ilamathi
  • 11
  • 1
  • This appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). If you used an AI tool to assist with any answer, I would encourage you to delete it, as [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was AI-generated, please leave feedback accordingly. The moderation team can use your help to identify issues. – NotTheDr01ds Jun 02 '23 at 17:43
0

just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file

Charpman
  • 69
  • 1
  • 10
0

For me occur when change the night mode, only write this in the manifest:

android:configChanges="uiMode"
meleAstur
  • 11
  • 2
0

Put this under AndroidManifest.xml

<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">

please let me know if it worked(It worked for me, I'm new to Android studio) I saw this code on web.

0

If you are using a "TextView" field to output answers it will reset on orientation changes.

Take note that "EditText" fields do not reset on orientation changes. with no additional code needed.

However, "android:inputType="none" does not work. "android:editable="false"" works but its depreciated.

I'm using the latest version of Android Studio (Bumblebee)

0

If you use this code in your Android Manifest file in Android Studio Bumblebee:

<activity android:name=".MainActivity"android:configChanges="keyboardHidden|orientation|screenSize">

You need additional code as this code above messes your layout up when you change orientation. However, it keeps the text/numbers in your choice of text input fields respectively.