Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application restarts on orientation change

Tags:

android

When I change orientation application restarts and I lost my current data.. I am using activity group which contain lots of activities when i change orientation application restarts from main activity.

is it possible to avoid this application restart on orientation change?

Anybody knows please let me know..

like image 721
Renuka Avatar asked Nov 02 '10 07:11

Renuka


People also ask

How do I stop Android from restarting activity when changing orientations?

If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.

What happens when orientation changes Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do you prevent the data from reloading and resetting when the screen is rotated?

Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.

How do you fix the orientation of an Android app?

You just have to define the property below inside the activity element in your AndroidManifest. xml file. It will restrict your orientation to portrait. Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.


5 Answers

Android restarts the activities whenever the orientation change by default.

You will need to save your data/state by calling onSaveInstanceState() before Android destroys the activities.

Have a look here: Handling Runtime Changes

This SO question also proves to be a good read in understanding how you could deal with it.

You could prevent this by adding android:configChanges="orientation" to your activity in AndroidManifest file.

Source: http://developer.android.com/guide/topics/manifest/activity-element.html#config

like image 144
SteD Avatar answered Nov 15 '22 13:11

SteD


If your android targetSdkVersion is 12 or less, add the android:configChanges="orientation|keyboardHidden" property to your android manifest.

If your android targetSdkVersion is 13 or more, use orientation|keyboardHidden|screenSize as the value for android:configChanges.

Overall it should look like this:

<activity android:name=".MyActivity" android:configChanges="<your value here>">

like image 37
Mohit Avatar answered Nov 15 '22 12:11

Mohit


You could tell the system to ignore the changes with the following:
<activity android:name="SomeActivity" android:configChanges="keyboardHidden|orientation">

but I would suggest not doing that because this is often an indication of underlying problems that are yet to emerge.

My advice is that you simply add a new class that will handle resuming of all long operations or any activity modifications.

like image 21
Vuk Avatar answered Nov 15 '22 14:11

Vuk


This is how it's supposed to work. There's a way to make it not do that, but you should be following the lifecycle and be able to handle activity restarts gracefully. This has been asked here many times.

like image 32
Falmarri Avatar answered Nov 15 '22 13:11

Falmarri


There is some good information in the API documentation on why the current Activity gets destroyed and rebuilt. I found it to be very enlightening the last time I worked on something related.

Unless you specify otherwise, a configuration change (such as a change in screen orientation[...]) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges

like image 44
dustmachine Avatar answered Nov 15 '22 12:11

dustmachine