Stefan Bossbaly
Stefan Bossbaly

Reputation: 6804

Proper Way of Handling an Orientation Change in Android

What is the proper way of handling an orientation change in Android? When I researched this question there are two methods that came up.

1st Method Use the methods onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState) to store and restore your Activity after being killed by the Android OS after the orientation change.

2nd Method Added android:configChanges="orientation|keyboardHidden" to your AndroidManifest.xml so the Activity will not be destroyed when the orientation is changed.

I have tried both methods and they both work, however the first method takes a lot longer to implement. While I do see posts about the 2nd method, I want to know if this is an "accepted" and "proper" way of handling an orientation change. And what are the advantages and disadvantages for each method? Thanks!

Upvotes: 7

Views: 519

Answers (2)

xevincent
xevincent

Reputation: 3734

See http://developer.android.com/guide/topics/resources/runtime-changes.html where they explain both methods and give the pros and cons and best solution.

Upvotes: 0

Alexandru Cristescu
Alexandru Cristescu

Reputation: 3948

The second method will not allow you to do certain orientation specific stuff such as load a different layout for when the screen is rotated or not (I'm thinking of resource suffixes here). I have not encountered any other ill effects, however the docs state that: "Using this attribute should be avoided and used only as a last-resort."

More info here: http://developer.android.com/guide/topics/resources/runtime-changes.html

Upvotes: 3

Related Questions