Reputation: 751
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?
Upvotes: 75
Views: 74749
Reputation: 11
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.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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)
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1662
To stop from destroying the activity on rotation
`android:configChanges="keyboardHidden|orientation|screenSize"`
Upvotes: 3
Reputation: 1243
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.
Upvotes: 106
Reputation: 11
For me occur when change the night mode, only write this in the manifest:
android:configChanges="uiMode"
Upvotes: 0
Reputation: 3407
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
}
}
Upvotes: 8
Reputation: 2219
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.
Upvotes: 1
Reputation: 1756
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.
Upvotes: 9
Reputation: 69
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
Upvotes: 0
Reputation: 3062
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"/>
Upvotes: 4
Reputation: 4301
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>
Upvotes: 21
Reputation: 7626
Add android:configChanges="keyboardHidden|orientation"
to your activity
Upvotes: 7
Reputation: 13846
Check in your android manifest file that you have written android:configChanges="orientation"
on the activity..
Upvotes: 10
Reputation: 4041
I would recommend using Fragments. You can simply use setRetainInstance(true)
to notify that you want to keep your fragment.
Upvotes: 6
Reputation: 12302
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
Upvotes: 2