【异常记录】Only fullscreen opaque activities can request orientation

今天在Android8.0上遇见一个奇葩问题Only fullscreen opaque activities can request orientation

一、出现场景
三星手机S8 android 8.0
targetSdkVersion 27
透明Activity
二、解决方案
manifest中移除android:screenOrientation=“portrait”

三、原因(源码中寻找)
查看Android 8.0源码

3.1、ActivityRecord#setRequestedOrientation
【异常记录】Only fullscreen opaque activities can request orientation
有几个条件:

非全屏 !fullscreen
targetSdkVersion的设置为大于26 appInfo.targetSdkVersion > O
ActivityInfo.isFixedOrientation(requestedOrientation)
所以下边来跟踪一下ActivityInfo.isFixedOrientation(requestedOrientation)

3.2、跟踪ActivityInfo.isFixedOrientation(requestedOrientation)
【异常记录】Only fullscreen opaque activities can request orientation【异常记录】Only fullscreen opaque activities can request orientation
最后的原因找到了,就是因为orientation == SCREEN_ORIENTATION_PORTRAIT

3.3、总结一下
如果一个 targetSdkVersion>26的Android App
运行在Android 8.0(含) 以上的设备上时
如果启动的Activity为透明Activity
screenOrientation 需为默认状态

四、Google为什么这么做?
在 * 中看到一句话。

这句话,我在google官方文档上并没有找到依据,但经过我的验证是正确的

If you use a fullscreen transparent activity, there is no need to specify the orientation lock on the activity. It will take the configuration settings of the parent activity. So if the parent activity has in the manifest:

这种情况下,透明Activity使用的是栈中上一层可见Activity的orientation设定。仔细想一想是合理的,因此这并不是一个bug。

五、最后重申一遍解决方案
综上所述,移除android:screenOrientation=“portrait” 的解决方案,完全符合Google的设计本意,并非Bug
————————————————
转载链接:https://blog.csdn.net/xiaxl/article/details/88894333