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 (and prevents orientation changes on other phones as well).
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 it seems to get restarted and loses all its states. This does not happen with the HTC Hero version.
My application is quite big, so I don't want it to restart and lose all its states when the keyboard is opened. How can I prevent that?
In Visual Studio Xamarin:
using Android.Content.PM;
to you activity namespace list.[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
as an attribute to you class, like that:
Use this..
You need to modify AndroidManifest.xml as Intrications (previously Ashton) mentioned and make sure the activity handles the onConfigurationChanged event as you want it handled. This is how it should look:
Add
to your manifest.
Please note, none of the methods seems to work now!
In Android Studio 1 one simple way is to add
android:screenOrientation="nosensor"
.This effectively locks the screen orientation.
Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:
Avoid reloading activity with asynctask on orientation change in android
Add
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.See Developer reference 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.
Update: As of Android 3.2, you also need to add "screenSize":
From Developer guide Handling the Configuration Change Yourself