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?
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?
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.
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>
Check in your android manifest file that you have written android:configChanges="orientation"
on the activity..
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.
I would recommend using Fragments. You can simply use setRetainInstance(true)
to notify that you want to keep your fragment.
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
}
}
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"/>
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
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.
To stop from destroying the activity on rotation
`android:configChanges="keyboardHidden|orientation|screenSize"`
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
For me occur when change the night mode, only write this in the manifest:
android:configChanges="uiMode"
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.