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..
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.
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.
Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.
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.
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
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>">
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With