android – Screen orientation and values in manifest.xml-ThrowExceptions

Exception or error:

I want to use all activities in my form in landscape or portrait.
When user select orientation – this is valid for all activities.
Tried with “behind” option orientation. According to Google – orientation will depend on previous activity.
My first activity use setRequestedOrientation to set selected from user orientation.
Next activities have to follow same orientation.
Do I have to put setRequestedOrientation in their code too? Or relly on ‘behind’ parameter in manifet? Putting setRequestedOrientation may be cause onCreate again?

Tried “portrait” and setRequestedOrientation() – resilt si onCreate was called 2 times.
Problem is in next activity -> because of “portrait” in first activity – android started next activity with same orientation. It ignores “landscape” orientation wich was set by me 🙁

How to solve:

If you want to have fixed orientation for your activities then you can use-



as an attribute to that activity in that manifest. But if you want to set the orientation runtime depending upon what was the previous orientation while launching the application, you need to check for the previous orientation in onCreate() and then set it to that value programmatically there itself using setRequestedOrientation()

As pointed by use android:screenOrientation="sensorPortrait" for a better support on tablets.

For more details on the different values for android:screenOrientation and what each of them do, look at the docs:


            android:screenOrientation="portrait" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


To handle the changes in orientation, add the following line in AndroidManifest.xml:
under <activity> tag as shown below:-

        ---------- >
        <activity android:name=".MainActivity"

Note: Whenever there is a change in the configuration such as orientation, screenLayout, screenSize, etc., the activity is restarted and its onCreate method is called. To prevent this, we must handle any changes in configuration.


Do it programmatically, for example in an Activity base class

I tryed its working mobile and tablet. use anyone portrait or landscape.

public void onCreate(Bundle savedInstanceState) {

manifest.xml inside application tag

        android:screenOrientation="portrait" />


        android:screenOrientation="landscape" />


From my experience I would recommend to setRequestedOrientation in every activity in onCreate method, onCreate won’t be called again it’s safe.


For now I put check in onCreate:

m_bSkip = (this.getRequestedOrientation() != MyApp.sInstance.GetScreenOrientation());
if (m_bSkip)

When I enter oncreate and screen orientation is not desired – exit.
When I enter in onCreate and screen orientation is desired one – continue with initialization.
This fixes situation without need of keeping async task related to activity and check for new activity.
Of course all functions: onStart,onResume,onPausde,onStop… have to check this flag to avoid null pointer exception.


go to Android Manifest editor , down there you will see
click on it , to the right – you will see a new window: scroll down and choose select orientation – “portrait”


In Android studio 3.6.0


will give you error you have to either specify

android:screenOrientation=”fullSensor” or android:screenOrientation=”unspecified”

fullSensor Means either you have ON the “Rotate off” or not it will change the orientation based on you move the phone

unspecified Means if you have ON the Rotate off then it will stay only in that orientation and if not then it will change the orientation based on you move the phone.

Leave a Reply

Your email address will not be published. Required fields are marked *