12

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..

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
Renuka
  • 783
  • 2
  • 9
  • 18

6 Answers6

13

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>">

MoralCode
  • 1,954
  • 1
  • 20
  • 42
Mohit
  • 807
  • 9
  • 11
11

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

Community
  • 1
  • 1
SteD
  • 13,909
  • 12
  • 65
  • 76
  • 1
    I was using activity groups and on each activity or group launch my application communicate with server and fetch data. So it was not possible to handle each activity's configure change. Instead i keep orientation fixed and use listener for orientation change and then use separate XML for landscape and portrait. This avoid restart of application and saves my data too. – Renuka Nov 25 '10 at 07:16
3

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.

Vuk
  • 1,225
  • 12
  • 15
1

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.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
1

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

dustmachine
  • 10,622
  • 5
  • 26
  • 28
0

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

Patratacus
  • 1,651
  • 1
  • 16
  • 19