最近在华为8.0系统的手机中遇到Only fullscreen opaque activities can request orientation
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
at android.app.Activity.onCreate(Activity.java:1081)
at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:85)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:149)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:313)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
at com.secoo.commonsdk.components.TrackableActivity.onCreate(TrackableActivity.kt:45)
at com.secoo.commonsdk.arms.base.BaseActivity.onCreate(BaseActivity.java:124)
at com.secoo.livevod.live.activity.LiveMoreDrawActivity.onCreate(LiveMoreDrawActivity.kt:48)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at com.qiyukf.unicorn.m.a.callActivityOnCreate(QiyuInstrumentation.java:258)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
一、 出现的场景
- Android 8.0
- Activity透明
- targetSdkVersion 27
二、解决方法
第一种解决方法
移除manifes
t中的Activity
对应的android:screenOrientation="portrait"
如果需要使用android:screenOrientation="portrait"
则可在对应的Activity
的onCreate
中添加:
//android O fix bug orientation
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
第二种解决方法
-
1.找到你设置透明的Activity,然后在他的
theme
中将android:windowIsTranslucent
改为false
eg:<item name="android:windowIsTranslucent">false</item>
- 再加入
<item name="android:windowDisablePreview">true</item>
就搞定了。
- 再加入
三、原因(源码中寻找)
3.1 、 ActivityRecord#setRequestedOrientation
1.jpeg有几个条件:
- 非全屏
!fullscreen
-
targetSdkVersion
的设置为大于26appInfo.targetSdkVersion > O
ActivityInfo.isFixedOrientation(requestedOrientation)
所以下边来跟踪一下ActivityInfo.isFixedOrientation(requestedOrientation)
3.2 、跟踪ActivityInfo.isFixedOrientation(requestedOrientation)
2.png继续跟踪isFixedOrientationPortrait(int orientation)
最后的原因找到了是因为orientation == SCREEN_ORIENTATION_PORTRAIT
这种情况下,透明Activity使用的是栈中,上一层可见Activity的orientation设定
网友评论