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 :
- I launch the app on the
MainActivityin portrait on a smartphone - I rotate the smartphone in landscape (the activity does not rotate)
- I click on a button in order to launch the
SecondActivity - 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.