68

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
  • 56,954
  • 26
  • 136
  • 173
chirag
  • 681
  • 1
  • 5
  • 3

14 Answers14

94

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,083
  • 7
  • 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
  • 3,935
  • 1
  • 30
  • 37
11

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

Mehul Ranpara
  • 4,136
  • 2
  • 23
  • 38
ninjasense
  • 13,382
  • 17
  • 71
  • 91
  • 1
    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
7

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

Avadhani Y
  • 7,276
  • 18
  • 55
  • 90
7

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,568
  • 1
  • 16
  • 19
6

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

Grimmace
  • 3,933
  • 1
  • 28
  • 25
5

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
  • 3,533
  • 4
  • 18
  • 31
Tushar patel
  • 2,874
  • 2
  • 22
  • 25
  • 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
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,115
  • 1
  • 21
  • 28
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
  • 14,336
  • 5
  • 53
  • 68
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
  • 1,747
  • 12
  • 26
1

To stop from destroying the activity on rotation

`android:configChanges="keyboardHidden|orientation|screenSize"`
Vivek Pratap Singh
  • 1,284
  • 15
  • 23
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.