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?

UPDATE:
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-

android:screenOrientation=”portrait”

android:screenOrientation="sensorPortrait"

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()

UPDATE:
As pointed by @s.co.tt 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:

https://developer.android.com/guide/topics/manifest/activity-element.html#screen

###

<activity
            android:name=".Android_mobile_infoActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

###

To handle the changes in orientation, add the following line in AndroidManifest.xml:
android:configChanges="orientation|screenSize"
under <activity> tag as shown below:-

   <application
        android:allowBackup="true"
        ----------
        ---------- >
        <activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize">
        ----------
        </activity>
        ----------
    </application>

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.

@Override 
public void onCreate(Bundle savedInstanceState) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   //or
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

manifest.xml inside application tag

 <activity
        android:name=".Login_Activity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />

or

 <activity
        android:name=".Login_Activity"
        android:label="@string/app_name"
        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)
  return;

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 MainActivity.java
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

android:screenOrientation=”portrait”

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 *