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’]



  1. There’s a little more going on here. Android assumes by default that your app knows nothing about screen rotations. That’s a pretty good assumption for current apps since Google didn’t even decide on how to handle it until recently. So by default Android does all that destroy/create stuff you mentioned.

    This will be fine for 90% of all applications so most developers will not have to worry about it. It’s even a useful way to test your application life cycle and state saving/restoring code. However for those apps that want to be smarter and optimize the transition, there are a couple of ways.

    The simplest way is to implement onRetainNonConfigurationInstance() to save some data that will be kept across the destroy. You use getLastNonConfigurationInstance() in the new instance of the Activity to recover that information. You can keep anything, even a reference to your current Intent or references to running Threads.

    The more complicated way is to use the android:configChanges attribute to tell Android which changes you can handle. For example if you set it to “keyboardHidden|orientation” then Android will NOT destroy and recreate your Activity when the user flips the keyboard. Instead, it will call onConfigurationChanged(Configuration).

    There’s a pretty good writeup about this in the latest 1.0 SDK documentation, see:

    See also related methods in that class.

    “Hello, Android: Introducing Google’s Mobile Development Platform” – now in 7th beta

  2. Thanks for the post and the comment referencing documentation. When I encountered this problem, I wrote a simple routine to grab all my saved data from a separate class. I can see my class idea as a memory hog but it worked at the time. Now I will implement the onRetainNonConfigurationInstance() approach.


  3. I’m late to the game on reading this article, but it is very good, and the method of illustrating the behavior is awesome. Thanks much for such a clear resource ! I’ve said it before, but it bears repeating, AndroidGuys ROCKS !

    • well Chrome does a better job than IE9 on rendering this site… lol couldn’t even see a single piece of code with IE9.
      Anyway i’m not installing firefox just for this site, i can read it with chrome wether it’s rendered correctly or not, just need to use these my reading capabilities to see where code starts and ends…

  4. is to sale products and services online…

    and what it takes to convince a prospective buyer. customers generally prefer to have post-sales services so that sustainability of their products can be ensured.most of the customers look for a designer who provides credible after-sales services. like…

  5. their secrets then they might not be…

    able to make so much money. some people that make an income from blogging may write an ebook and sell itas mentioned before, being a millionaire blogger comes from a large number of blogs and at some point the blogger would…

Comments are closed.