views:

5061

answers:

1

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation="portrait" in the manifest xml. This works ok for the htc magic phone.

But I have a problem with the HTC G1 phone as i open the hardware qwerty keyboard (not the virtual keyboard). My activity stays in portrait mode, but seems to get restarted and looses all its states. This does not happen with the hero version.

My application is quite big so i dont want it to restart and loose all its states when the keyboard is opened. Any idea on how i can do that?

+15  A: 

Try adding android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation">

See http://developer.android.com/reference/android/R.attr.html#configChanges for more details.

However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.

AshtonBRSC
Just to add to this and be really explicit, Android can mercilessly kill your application at any time, regardless of orientation changes, so you should use onPause() and onSaveInstanceState() to save state, no matter what.
Klondike