今天讲的是target升级为27后高频出现的crash,Only fullscreen opaque activities can request orientation的crash,当targetsdk>=27的时候,且手机Android系统是26,27的时候会crash(华为和小米8.1自己做了优化所以不会出现这个问题,其他品牌手机没有测试)。因为开发的是SDK,所以在适配上很是无奈,SDK提供给使用方,不能限制使用方的targetSdk。如果是APP开发,targetsdk随意设置的话,以下这个仅供参考建议,下面我们就开始分析和解决之路。
一、分析源码,寻找crash源头
step1:targetsdk为26 Activity的onCreate方法源码
if (getApplicationInfo().targetSdkVersion > O && mActivityInfo.isFixedOrientation()) {
final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
ta.recycle();
if (isTranslucentOrFloating) {
throw new IllegalStateException(
"Only fullscreen opaque activities can request orientation");
}
}
从26的源码中可看出,当targetsdk>26,设置了屏幕方向,并且是透明的会报上述错误。证明在Android8.0手机上应用如果是targetsdk>26,设置了透明和屏幕方向则会挂掉
step2:targetsdk为27 Activity的onCreate方法源码
if (getApplicationInfo().targetSdkVersion >= O_MR1 && mActivityInfo.isFixedOrientation(){
final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
ta.recycle();
if (isTranslucentOrFloating) {
throw new IllegalStateException(
"Only fullscreen opaque activities can request orientation");
}
}
从27的源码中可看出,当targetsdk>=27,设置了屏幕方向,并且是透明的会报上述错误。证明在Android8.1手机上应用如果是targetsdk>=27,设置了透明和屏幕方向则会挂掉
step3:targetsdk为28 时,去掉了设置方向和透明冲突crash代码
targetsdk为28 Activity的onCreate方法源码中去掉了上述代码,不做限制。
step4:解释透明条件和固定方向
具体的什么是透明呢,看源码isTranslucentOrFloating,符合以下条件:
/**
* Determines whether the {@link Activity} is considered translucent or floating.
* @hide
*/
public static boolean isTranslucentOrFloating(TypedArray attributes) {
final boolean isTranslucent = attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent,
false);
final boolean isSwipeToDismiss = !attributes.hasValue( com.android.internal.R.styleable.Window_windowIsTranslucent)
&& attributes.getBoolean(com.android.internal.R.styleable.Window_windowSwipeToDismiss, false);
final boolean isFloating =attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating,
false);
return isFloating || isTranslucent || isSwipeToDismiss;
}
什么是固定方向呢?
/**
* Returns true if the activity's orientation is fixed.
* @hide
*/
public boolean isFixedOrientation() {
return isFixedOrientationLandscape() || isFixedOrientationPortrait()
|| screenOrientation == SCREEN_ORIENTATION_LOCKED;
}
满足以下条件,就是满足固定方向
orientation == SCREEN_ORIENTATION_PORTRAIT (1)
|| orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT (7)
|| orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT (9)
|| orientation == SCREEN_ORIENTATION_USER_PORTRAIT; (12)
orientation == SCREEN_ORIENTATION_LANDSCAPE (0)
|| orientation == SCREEN_ORIENTATION_SENSOR_LANDSCAPE (