一文分析 Only fullscreen opaque activities can request orientation 报错原因及解决方案

今天在三星S8上遇见一个奇葩问题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

Alt text

有几个条件:

  • 非全屏 !fullscreen
  • targetSdkVersion的设置为大于26 appInfo.targetSdkVersion > O
  • ActivityInfo.isFixedOrientation(requestedOrientation)

所以下边来跟踪一下ActivityInfo.isFixedOrientation(requestedOrientation)

3.2、跟踪ActivityInfo.isFixedOrientation(requestedOrientation)

在这里插入图片描述

继续跟踪isFixedOrientationPortrait(int orientation)

在这里插入图片描述

最后的原因找到了,就是因为orientation == SCREEN_ORIENTATION_PORTRAIT

3.3、总结一下

  • 如果一个 targetSdkVersion>26的Android App
  • 运行在Android 8.0(含) 以上的设备上时
  • 如果启动的Activity为透明Activity

screenOrientation 需为默认状态

四、Google为什么这么做?

stackoverflow 中看到一句话。

这句话,我在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

========== THE END ==========

wx_gzh.jpg

posted @ 2020-03-02 16:01  bjxiaxueliang  阅读(9122)  评论(0编辑  收藏  举报