Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 258
Stefan Bossbaly Avatar asked Jun 27 '11 15:06

Stefan Bossbaly


1 Answers

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

like image 128
Alexandru Cristescu Avatar answered Nov 03 '22 01:11

Alexandru Cristescu