赞
踩
crash信息:
- Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
- at android.app.Activity.onCreate(Activity.java:1081)
- at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:278)
- at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
- at arm.sankuai.moma.base.AbstractActivity.onCreate(AbstractActivity.kt:31)
- at arm.sankuai.moma.base.BaseActivity.onCreate(BaseActivity.java:26)
- at arm.sankuai.moma.poi.poi_list.SeaBaseActivity.onCreate(SeaBaseActivity.java:60)
- at arm.sankuai.moma.poi.poi_list.PublicSeaActivity.onCreate(PublicSeaActivity.java:34)
- at android.app.Activity.performCreate(Activity.java:7372)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
- ... 9 more
crash原因:
- @MainThread
- @CallSuper
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
- // target 大于26 指定方向
- 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");
- }
- }
- Activity.java
- ------------------------------------------------
- ActivityInfo.java
- /**
- * 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 specified orientation is considered fixed.
- * @hide
- */
- static public boolean isFixedOrientation(int orientation) {
- return isFixedOrientationLandscape(orientation) || isFixedOrientationPortrait(orientation);
- }
-
- /**
- * Returns true if the activity's orientation is fixed to landscape.
- * @hide
- */
- public static boolean isFixedOrientationLandscape(@ScreenOrientation int orientation) {
- return orientation == SCREEN_ORIENTATION_LANDSCAPE
- || orientation == SCREEN_ORIENTATION_SENSOR_LANDSCAPE
- || orientation == SCREEN_ORIENTATION_REVERSE_LANDSCAPE
- || orientation == SCREEN_ORIENTATION_USER_LANDSCAPE;
- }
-
- /**
- * Returns true if the activity's orientation is fixed to portrait.
- * @hide
- */
- public static boolean isFixedOrientationPortrait(@ScreenOrientation int orientation) {
- return orientation == SCREEN_ORIENTATION_PORTRAIT
- || orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT
- || orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT
- || orientation == SCREEN_ORIENTATION_USER_PORTRAIT;
- }
-
- // Oreo 8.1.0_r33源码如下:
- @MainThread
- @CallSuper
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
- // 发现代码删除,bug修复
- if (mLastNonConfigurationInstances != null) {
- mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
- }

结论:
Activity方向为:Portrait、 Landscape(以及相关的方向)和SCREEN_ORIENTATION_LOCKED
activity样式为:R.styleable.Window_windowIsTranslucent、R.styleable.Window_windowSwipeToDismiss或者R.styleable.Window_windowIsFloating
target 大于26
以上条件满足时会在activity onCreate方法中触发Crash,且只在8.0机器上出现。
适配方案
1、不指定方向
2、不在Manifest文件中指定方向,通过代码根据系统版本决定是否指定方向
- public static void fixedOrientation(@NonNull Activity activity) {
- if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
- activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。