Arpit
Arpit

Reputation: 654

Changing the Orientation Smoothly for SDK from App

I know the question related to orientation has been asked multiple times, but I am facing some weird issue. I am working on one SDK that has some Activity which can be opened from the App, The SDK asks for the orientation values from the App and I am updating the orientation in onCreate() of the Activity by setting:

requestedOrientation = orientation

It works perfectly when I am holding the device in the same orientation as the orientation is set by App. But If I am holding the device in portrait mode and setting the requestedOrientation to LANDSCAPE then my screen flickers, 1st it shows the portrait screen and then goes to landscape. In manifest I have set the configChanges to android:configChanges="keyboard|keyboardHidden|screenSize" I have separate layouts for both the orientations. Let me know, if anything else is also required.

Upvotes: 2

Views: 759

Answers (3)

Arpit
Arpit

Reputation: 654

We have implemented a solution at our end as below, Now the clients are declaring the same Activity in their manifest file and replacing the orientation as per their requirements.

SDK Manifest:

<activity
        android:name=".ActivityName"
        android:configChanges="keyboard|keyboardHidden|screenSize"
        android:exported="false"
        android:label="@string/title_activity_login"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" />

Client Manifest:

<activity
        android:name="sdkPackage.ActivityName"
        tools:replace="android:screenOrientation"
        android:screenOrientation="sensorLandscape"/>

sdkPackage is the complete package of SDK, basically the client is now overriding the android:screenOrientation and setting it to whatever orientation they want.

Upvotes: 0

Zain
Zain

Reputation: 40898

But If I am holding the device in portrait mode and setting the requestedOrientation to LANDSCAPE then my screen flickers, 1st it shows the portrait screen and then goes to landscape

The screen orientation is an activity feature that can differ from activity to another. So, going from activity to another, the system has to decide ahead which orientation it should display the activity on; this is taken before requestedOrientation takes place; because it shows the activity with the default orientation setting; then when requestedOrientation get called, the activity is recreated with the desired orientation; so onCreate() will get called twice with different orientations; and that is why the screen flickers with the portrait orientation (the system desired one) before showing the landscape orientation (the user desired one).

In documentation, the default android:screenOrientation orientation is "unspecified"; i.e., it's left up to the system to decide;

It works perfectly when I am holding the device in the same orientation as the orientation is set by App.

Usually that defaults up to the device sensor.

But you can specify that to be "locked" for the second activity ahead (in the manifest's activity section) so that the system has no chance to decide that.

"locked": Locks the orientation to its current rotation, whatever that is. Added in API level 18.

<activity
    android:name="....."
    android:screenOrientation="locked"
    .... />

Upvotes: 2

Sumit Kumar
Sumit Kumar

Reputation: 292

You should set requested orientation based on configuration changes

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if...
}

If you want to support portrait and reverse portrait together, do it with SCREEN_ORIENTATION_SENSOR_PORTRAIT.

Upvotes: 0

Related Questions