3

I have been going gaga to figure this out.
Although I have read a lot that on Orientation Change, Android kills an activity and starts it as a fresh one, and the only way to handle this is to save all the stuff inside onSaveInstanceState() and try to restore it inside onCreate().

But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and complex scenarios.

Is there any simple way to just point Android that this activity doesn't need to be redrawn at all when the orientation is changed so that it automatically saves all the data and re-uses it?

I wonder if there's any thing like that.

Aman Alam
  • 11,231
  • 7
  • 46
  • 81

8 Answers8

11

Yes, you can add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.

EDIT: The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really necessary. For example the Camera application uses this attribute because it the camera preview screen mustn't be recreated when an orientation change happens. Users expect the camera preview to work without any delays when they rotate their devices and camera initialization is not a very fast process. So it's kind of a native behavior for the Camera application to handle orientation changes manually.

For most applications it doesn't really matter if an activity is recreated or not during orientation changes. But sometimes it's more convenient to persist an activity during this process because of slow activity creation, asynchronous tasks performed by an activity or some other reasons. In this case it's possible to tweak an application a little and to use the android:configChanges="orientation" attribute. But what is really important to understand when you use this tweak is that you MUST implement methods for saving and restoring a state properly!

So to sum up this answer, the android:configChanges can allow you to improve the performance of an application or to make it behave "natively" in some rare cases but it doesn't reduce the amount of code you have to write.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • adding android:configChanges="orientation" solves the problem, but this blog >http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html says that it isn't a good practice that can cause troubles later. reading more of it now. – Aman Alam May 09 '11 at 08:00
  • 1
    I can't find any explanation there for why this is bad. I think it's OK to use this method if you have only one layout file for the activity. And I think that's the most optimal way to change orientation's activity, because activity re-creation is surely slower, even if you'd use `onRetainNonConfigurationInstance()`. – Michael May 09 '11 at 08:09
  • 1
    @Pixie: "I can't find any explanation there for why this is bad." -- it is bad because @Sheikh Aman will experience serious pain in the future. With this change, the OP will need to manually reload each and every individual resource that changes as a result of the configuration change. That will include resources that are not obvious *how* to manually reload, such as dimension resources. – CommonsWare May 09 '11 at 10:40
  • I understand it and that's why I said that it's OK to use this method if there's no need to change layout depending on activity orientation. – Michael May 09 '11 at 10:43
  • Actually.. I noticed that the onDraw method is still called when I change orientation. The thing is.. I don't care about the way the user holds the device. I hope this answer gets expanded. – baash05 Oct 15 '11 at 11:34
  • In the original question "activity doesn't need to be redrawn" means that there's no need to change its layout. – Michael Oct 15 '11 at 13:29
  • I got it working.. thank you Pixie. I too don't understand why it would be bad. I want my layout to stay landscape, and reloading an app where an ONSIZE would have done the job, boggles my mind. – baash05 Oct 17 '11 at 22:16
  • It's not so bad if an activity can persist its state during configuration changes. But if an activity is poorly designed and it can't manage its state properly then it's not a good idea to rely on this behavior because later you might need to recreate the activity on configuration changes and it may become a problem. – Michael Oct 18 '11 at 06:22
  • So the android OS assumes that everyone is too incompetent to handle their GUI. That sounds dirty. :) But it makes sense. It could be the reason they favor the use of XML to handle the GUI.. – baash05 Oct 19 '11 at 21:24
  • I added some information to the answer and I hope it will be helpful. – Michael Oct 20 '11 at 07:37
6

But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and complex scenarios.

Then move that logic out of the activity and into a service.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Exactly. This would be the best way, but may perhaps require some more time and effort. I'll surely look into this one. Thanks Mark! :) Another great answer for me :) – Aman Alam May 09 '11 at 10:54
4

Yes, you can add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.

IMHO, it's better to declare android:configChanges="orientation|keyboard|keyboardHidden"

About the blog post you gave the link in another answers. I guess here is the answer:

If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the Activity restart, then you can declare that your Activity handles the configuration change itself, which prevents the system from restarting your Activity.

I spoke as well with an android developer about this problem. And he meant following. If you don't have different layouts for landscape and portrait orientation, you can easy use configChanges.

Apurv
  • 3,723
  • 3
  • 30
  • 51
Tima
  • 12,765
  • 23
  • 82
  • 125
2

I solved my problem by adding this to my activity in my manifest file

android:configChanges="keyboardHidden|orientation|screenSize"

Devendra Singh
  • 81
  • 1
  • 2
  • 9
1

Just answered this question earlier: Android - screen orientation reloads activity

In your case you want to completely prevent Android from killing your Activity. You'll need to update your manifest to catch the orientation change, then implement the orientation change callback to actually do whatever you need to do (which may be nothing) when an orientation change occurs.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Yes, it will work, but this blog -> http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html says that it isn't a good practice that can cause troubles later – Aman Alam May 09 '11 at 08:01
  • If you have logic to serialize your entire state to a `Bundle` and rebuild it from the `Bundle` in `onCreate` (and you have no persistent state like network or bluetooth connections) then I'd recommend using `onSaveInstanceState`. If you have network/bluetooth connections or similar resources that can not be trivially recreated then your choices are either don't kill the Activity as I've outlined above or use `onRetainNonConfigurationInstance()`. If you use the second you will have to make sure you can rebuild your UI into the proper state based on the object you retained. You have options. – Femi May 09 '11 at 08:11
1

android:screenOrientation="portrait" in the activity tag in the manifest will lock your orientation.

Check this link for more inforation.

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
Vaayu
  • 466
  • 4
  • 8
1

if you are doing a lot of networking inside Asynchronous task maybe you should use onRetainNonConfigurationInstance() ans then get the data back in your onCreate() method like this tutorial or this

Amal
  • 971
  • 9
  • 25
  • the first contain full explanation the second is just trivial example if you read the first then skip the other one – Amal May 09 '11 at 08:05
1

add android:configChanges="orientation" to your activity in manifest and add this code in your activity class and check..i hope it will help for you.

@Override
public void onConfigurationChanged(Configuration newConfig)
    {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.main);
}

this method will be called when orientation is changed nothing else if u don't want to change anything let it be blank

Dhruv
  • 1,129
  • 2
  • 13
  • 32
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133