setRequestedOrientation allows the rotation during 1 second

Viewed 1139

I am currently working on an Android application in which one I would like to :

  • disable the rotation on smartphone ;
  • enable the rotation on tablet.

In order to to do that, I cannot set the rotation directly into the manifest so I use the setRequestedOrientation method directly into the the onCreate method of my activity.

It works. But I have a case I cannot cover :

  1. I launch the app on the MainActivity in portrait on a smartphone
  2. I rotate the smartphone in landscape (the activity does not rotate)
  3. I click on a button in order to launch the SecondActivity
  4. the new activity is launched in landscape and then rotate to portrait

Is there a way to cover this case ? Is there a way to open the SecondActivity directly with the portrait orientation on smartphone without this rotation ?

Here some code from a very simple sample :

The MainActivity :

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        button?.setOnClickListener {
            startActivity(Intent(this, SecondActivity::class.java))
        }
    }
}

The SecondActivity :

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
}

The manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.myapplication">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity
                android:name=".MainActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"
        >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
                android:name=".SecondActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"
        />
    </application>

</manifest>

Thank you in advance for your help.

2 Answers
Yes set the orientation before setContentView

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.activity_second)

I had the same issue, here's my solution:

BaseActivity:

class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        lockPortraitForPhones()
        super.onCreate(savedInstanceState)
    }

    fun lockPortraitForPhones() {
        requestedOrientation = if (!UiUtil.isTablet(this)) {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            ActivityInfo.SCREEN_ORIENTATION_SENSOR
        }
    }
}

Provide your own isTablet implementation

Then inherit you activity from BaseActivity and lock the orientation of it. Ex:

class YourActivity : BaseActivity() 

The manifest:

     <?xml version="1.0" encoding="utf-8"?>

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