How to avoid restarting activity when orientation changes on Android
AndroidRotationScreenAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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.
Solution 2 - Android
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>
Solution 3 - Android
Check in your android manifest file that you have written android:configChanges="orientation"
on the activity..
Solution 4 - Android
Add android:configChanges="keyboardHidden|orientation"
to your activity
Solution 5 - Android
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
}
}
Solution 6 - Android
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.
Solution 7 - Android
I would recommend using Fragments. You can simply use setRetainInstance(true)
to notify that you want to keep your fragment.
Solution 8 - Android
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"/>
Solution 9 - Android
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
Solution 10 - Android
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.
Solution 11 - Android
> To stop from destroying the activity on rotation
`android:configChanges="keyboardHidden|orientation|screenSize"`
Solution 12 - Android
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
Solution 13 - Android
For me occur when change the night mode, only write this in the manifest:
android:configChanges="uiMode"
Solution 14 - Android
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.
Solution 15 - Android
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)
Solution 16 - Android
If you use this code in your Android Manifest file in Android Studio Bumblebee:
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.