Recipe Forcing Screen Orientation

Any Android device with an accelerometer can determine which way is down. As the device is tilted from portrait to landscape mode, the default action is to rotate the application view accordingly. However, as seen from the "Other Lifecycle Functions" recipe, the activity is destroyed and restarted on screen orientation changes.When this happens, the current state of the activity might be lost, disrupting the user experience.

One option to handle screen orientation changes gracefully is to save state information before the change and restore information after the change. A simpler method that might be useful is to force the screen orientation to stay constant. For each activity in the AndroidManifest, the screenOrientation can be specified. For example, to specify that the activity always stays in portrait mode, the following can be added to the activity element:

android:screenOrientation="portrait"

Similarly, landscape mode can be specified using the following: android:screenOrientation="landscape"

However, the previous still causes the activity to be destroyed and restarted when a hard keyboard is slid out.Therefore, a third method is possible:Tell the Android system that the application should handle orientation and keyboard slide-out events. This is done by adding the following attribute to the activity element:

android:configChanges="orientation|keyboardHidden"

This can be used alone or in combination with the screenOrientation attribute to specify the required behavior to the application.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment