Android application restarts on orientation change
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 i开发者_如何转开发t possible to avoid this application restart on orientation change?
Anybody knows please let me know..
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>">
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
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
It's Year 2022 and I'm here on this question. I just want to add to all the other answers here so other people who are here now have more examples. I found that adding configuration change to the manifest is the best way to handle the application resetting itself when orientation change. I like @Vuk answer the best for showing how to add it to the manifest.
I have this in my AndroidManifest.xml. I include screenLayout and screenSize for the older android version:
<activity
android:name=".MainActivity"
android:configChanges="screenLayout|orientation|screenSize"
android:exported="true">
</activity>
To handle the configuration change manually when you added to the manifest, add the following codes in the MainActivity.kt as an example of handling the screen orientation change. This just shows the toaster popup message, but you can handle it however you want.
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
//override stuff with orientation change, etc.
// Checks the orientation of the screen
if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show()
} else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show()
}
}
Reference: https://developer.android.com/guide/topics/resources/runtime-changes
精彩评论