Only fullscreen opaque activitie
错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app/com.test.app.TestActivity}:
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
at android.app.Activity.onCreate(Activity.java:1038)
at com.test.app.TestActivity.onCreate(TestActivity.java:59)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
原因:
在API版本O的Activity.java的onCreate方法中增加了如下代码:
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
//*********如下:
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");
}
}
//*********
if (mLastNonConfigurationInstances != null) {
mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
}
...
}
同样,在ActivityRecord.java中的setRequestedOrientation方法中也增加了如下代码
void setRequestedOrientation(int requestedOrientation) {
if (ActivityInfo.isFixedOrientation(requestedOrientation) && !fullscreen
&& appInfo.targetSdkVersion >= O) {
throw new IllegalStateException("Only fullscreen activities can request orientation");
}
...
}
注意:在在API版本O之后,Activity.java和ActivityRecord.java中又去掉了这段代码,所以该bug只会在8.0中产生
分析:
从上文代码可知,如果targetSdkVersion >= O且isFixedOrientation(固定方向),最后还设置了isTranslucentOrFloating(透明或悬浮),就会产生该bug。
解决办法:
1.设置targetSdkVersion < 26
既然设置targetSdkVersion>= O就会报错,那就设置targetSdkVersion < 26,但不推荐这样做,毕竟版本只会越更新越高,退回去没必要。
2.在透明或悬浮的Activity上不锁定屏幕方向(或在锁定屏幕方向的Activity上取消透明或悬浮设置)
isFixedOrientation()方法的代码如下,
public boolean isFixedOrientation() {
return isFixedOrientationLandscape() || isFixedOrientationPortrait()
|| screenOrientation == SCREEN_ORIENTATION_LOCKED;
}
根据代码可知横屏,竖屏或锁定屏幕就是固定屏幕,取消该设置就行。
同理,isTranslucentOrFloating()方法如下:
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;
}
根据代码,如必须固定activity方向,在更改如上三个设置即可。
3.针对8.0做修改
既然该问题只出现在8.0上,那我们就可以只对8.0作出修改,在activity中添加如下代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//如果是8.0,且是透明或悬浮activity,将方向设置为非锁定状态
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
fixOrientation();
}
super.onCreate(savedInstanceState);
...
}
//如果是8.0,设置方向时直接返回
@Override
public void setRequestedOrientation(int requestedOrientation) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
return;
}
super.setRequestedOrientation(requestedOrientation);
}
//判断是否是透明或悬浮
private boolean isTranslucentOrFloating() {
boolean isTranslucentOrFloating = false;
try {
int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
final TypedArray ta = obtainStyledAttributes(styleableRes);
Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
m.setAccessible(true);
isTranslucentOrFloating = (boolean) m.invoke(null, ta);
m.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
return isTranslucentOrFloating;
}
//将方向设置为非锁定状态
private void fixOrientation() {
try {
Field field = Activity.class.getDeclaredField("mActivityInfo");
field.setAccessible(true);
ActivityInfo o = (ActivityInfo) field.get(this);
o.screenOrientation = -1;
field.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
参考
8.0增加该段代码的提交记录
https://github.com/aosp-mirror/platform_frameworks_base/commit/39791594560b2326625b663ed6796882900c220f#diff-a9aa0352703240c8ae70f1c83add4bc8
9.0删除该段代码的提交记录
https://github.com/aosp-mirror/platform_frameworks_base/commit/e83f34cde79c51efb66f31f2770c2e8e572e96db#diff-a9aa0352703240c8ae70f1c83add4bc8
参考的大佬文章
https://blog.csdn.net/starry_eve/article/details/82777160