I am currently doing screen orientation tests for Activities. I found something interesting and would like to ask.
I have a MainActivity and a SubActivity, and the device’s orientation is set to Auto-rotate. In the AndroidManifest.xml, the orientation of each Activity is defined as follows:
<activity
android:name=".SubActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In this case, when I launch SubActivity from MainActivity using startActivity()
and then rotate the device, the MainActivity is fixed to Landscape because it is set to sensorLandscape
. However, SubActivity is not fixed and changes orientation according to the device’s rotation.
But when I add the following attribute of the to the theme of SubActivity, the result is different:
theme.xml
<style name="Theme.Transparent" parent="Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item> or <item name="android:windowIsFloating">true</item>
</style>
AndroidManifest.xml
<activity
android:theme="@style/Theme.Transparent"
android:name=".SubActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In this case, when I launch SubActivity from MainActivity using startActivity()
and then rotate the device, SubActivity follows the orientation of MainActivity.
Why does SubActivity follow the orientation of MainActivity when
windowIsTranslucent or windowIsFloating is applied to SubActivity? The only hint I found is that the requestedOrientation in SubActivity is logged as -1 (SCREEN_ORIENTATION_UNSPECIFIED
).
I am super curious if the reason SubActivity follows MainActivity’s orientation is related to ActivityManager
, ActivityRecord
, ActivityStack
or WindowManager
.
Where can I check the code for this problem?