[源码探究]Only fullscreen opaque activities can request orientation 缘由及解决方案

今天在三星S8上碰见一个奇葩问题Only fullscreen opaque activities can request orientationjava

1、出现场景

  • 三星手机S8 android 8.0
  • targetSdkVersion 27
  • 透明Activity

2、解决方案

manifest中移除android:screenOrientation="portrait"android

3、缘由(源码中寻找)

查看Android 8.0源码app

3.一、ActivityRecord#setRequestedOrientation

Alt text

有几个条件:google

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

因此下边来跟踪一下ActivityInfo.isFixedOrientation(requestedOrientation)spa

3.二、跟踪ActivityInfo.isFixedOrientation(requestedOrientation)

在这里插入图片描述

继续跟踪isFixedOrientationPortrait(int orientation)设计

在这里插入图片描述

最后的缘由找到了,就是由于orientation == SCREEN_ORIENTATION_PORTRAIT3d

3.三、总结一下

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

screenOrientation 需为默认状态code

4、Google为何这么作?

stackoverflow 中看到一句话。cdn

这句话,我在google官方文档上并无找到依据,但通过个人验证是正确的blog

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。

5、最后重申一遍解决方案

综上所述,移除android:screenOrientation="portrait" 的解决方案,彻底符合Google的设计本意,并不是Bug

相关文章
相关标签/搜索