Aman Aalam
Aman Aalam

Reputation: 11251

Forcing Android to not redraw activity on orientation change

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.

Upvotes: 3

Views: 15490

Answers (8)

Tima
Tima

Reputation: 12905

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.

Upvotes: 4

Vaayu
Vaayu

Reputation: 476

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

Check this link for more inforation.

Upvotes: 1

Devendra Singh
Devendra Singh

Reputation: 81

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

android:configChanges="keyboardHidden|orientation|screenSize"

Upvotes: 2

Michael
Michael

Reputation: 54705

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.

Upvotes: 11

CommonsWare
CommonsWare

Reputation: 1006539

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.

Upvotes: 6

Niranj Patel
Niranj Patel

Reputation: 33238

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

Upvotes: 1

Amal
Amal

Reputation: 971

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

Upvotes: 1

Femi
Femi

Reputation: 64690

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.

Upvotes: 1

Related Questions