Why is onCreate called twice when I rotate the screen from landscape to portrait?

Possible duplicate:
Problems handling orientation changes

I have a very simple activity ...

public class ShowOrientation extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d("ShowOrientation", "Orientation: " + getOrientation()); } private String getOrientation() { int o = getResources().getConfiguration().orientation; if (o == 1) return "PORTRAIT"; else if (o == 2) return "LANDSCAPE"; else return "*UNKNOWN!*"; } } 

When I run Activity in the emulator, it starts in PORTRAIT mode. When I rotate the screen (using the 9 button on the numeric keypad), I see one entry in my log file ...

 Orientation: LANDSCAPE 

Now, if I turn the screen back to PORTRAIT mode (using the 7 button on the numeric keypad), this is what I see in my log file ...

 Orientation: LANDSCAPE Orientation: PORTRAIT 

And then if I go back to LANDSCAPE again, I see this ...

 Orientation: LANDSCAPE 

Since this logging is done in onCreate (Bundle), I can only assume that my activity is destroyed / created twice upon rotation from the LANDSCAPE screen orientation to PORTRAIT and only once upon transition from PORTRAIT to LANDSCAPE!

I'm still new to Android, so I might miss something about the activity life cycle, but it seems wrong to me that the action is created or destroyed twice when the screen rotates in one direction, but only once when the screen is rotated in another way.

Did I find a mistake? Should this work like that? Or do I not know anything about the activity life cycle that explains this?

EDIT: Forgot to mention, it uses Android 2.2 api, Eclipse 3.5.2 and Android SDK Rev. 7.

+4
source share

All Articles