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

简介: 今天在三星S8上遇见一个奇葩问题`Only fullscreen opaque activities can request orientation`,探究一下Android源码,出现这个错误的原因,以及解决方案。

今天在三星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 =

文章首发于公众号”CODING技术小馆“,如果文章对您有帮助,可关注我的公众号。
文章首发于公众号”CODING技术小馆“,如果文章对您有帮助,可关注我的公众号。
文章首发于公众号”CODING技术小馆“,如果文章对您有帮助,可关注我的公众号。

目录
相关文章
|
4月前
|
Java 网络安全 Maven
简记:一个flutter构建错误A problem occurred configuring project ‘:smart_auth‘. > Could not res
简记:一个flutter构建错误A problem occurred configuring project ‘:smart_auth‘. > Could not res
93 0
|
Java
Preference跳转activity出错Unable to find explicit activity class
使用Preference可以非常方便的实现类似设置页面这样的菜单布局,甚至可以不需写java代码。那么可以在Preference中直接添加页面跳转么?其实非常简单,在Preference添加intent标签即可
473 0
|
API
Should we still build application buffer on top of CDS view
Should we still build application buffer on top of CDS view
84 0
Should we still build application buffer on top of CDS view
cannot open layout editor - how to fix error message
cannot open layout editor - how to fix error message
cannot open layout editor - how to fix error message
|
Android开发
android8.0采坑 Only fullscreen opaque activities can request orientation
android8.0采坑 Only fullscreen opaque activities can request orientation 也就是说只有全屏不透明的activity才可以设置方向,既然知道问题所在就好办了。
2991 0
|
Android开发
Android Studio 解决Error:(781) Multiple substitutions specified in non-positional format; did you ...
异常信息: /Users/mazaiting/AndroidStudioProjects/WisdomSite/app/build/intermediates/res/merged/debug/values/values.
1423 0
|
安全 Java Android开发
PhotoSharing Part I: Setting up the Photo Sharing Android Application
We will build a photo sharing Android app with real-time image uploading and downloading functionality using Alibaba Cloud OSS.
1759 0