Rotational Forces…On Your Android App

There have been several messages recently on the Android Google Groups related to rotation — the act of moving the display from portrait to landscape or vice versa. In the case of the T-Mobile G1, rotation should occur as part of sliding open the keyboard to do text entry. Today, we’ll take a peek at how rotation and the activity lifecycle work in concert, and what that means for you, the intrepid Android developer.

Some developers have been mystified with their application’s behavior when the display is rotated. Specifically, initialization code in onCreate() appears to happen every time the screen rotates.

The good news is that these developers are not crazy. onCreate() is called every time the screen rotates…because on a screen rotation, the activity is destroyed and created anew with access to the appropriate set of resources.

To examine this, let’s build a test application containing two fairly trivial activities. One (“parent”) has a button that launches the other (“child”), whereas the second has a button that calls finish() on itself.

The layouts of the two are all but identical, varying only in the label of the button:

[sourcecode language=’xml’]


About author

AndroidGuys 4641 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

GameStop tablets available now, bundled with six games

Buy one of the three tablets and receive a handful of games at no additional cost.

App Reviews

Multitasking Pro aims to bring more functionality to your device

If you’re a power user like myself, you may notice that stock and most manufacturer versions of Android do not let you have two programs on your screen at once.

News and Rumors

Rumor: Google Voice via Gmail Being Tested

It’s being rumored that Google is testing their Google Voice services and features through integration with Gmail.  Rather than relying on a phone to make calls, users would see a