java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

前端 未结 30 1474
难免孤独
难免孤独 2020-12-02 04:43

I am facing the problem while retrieving the contacts from the contact book in Android 8.0 Oreo java.lang.IllegalStateException: Only fullscreen opaque activities can reques

相关标签:
30条回答
  • 2020-12-02 05:38

    I faced this problem only in SDK 26 (8.0.0) if using windowIsTranslucent 'true' and forcefully setting orientation:

    Here's the solution:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);   
    
            // fixing portrait mode problem for SDK 26 if using windowIsTranslucent = true
            if (Build.VERSION.SDK_INT == 26) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }        
        }
    
    
    
           <style name="SplashActivty" parent="AppTheme">
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    
    
    
    <!-- Splash screen -->
            <activity
                android:name="edu.aku.family_hifazat.activities.SplashActivity"
                android:label="@string/app_name"
                android:screenOrientation="unspecified"
                android:theme="@style/SplashActivty">
    
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    
            </activity>
    
    0 讨论(0)
  • 2020-12-02 05:39

    The only solution that really works :

    Change:

    <item name="android:windowIsTranslucent">true</item>
    

    to:

    <item name="android:windowIsTranslucent">false</item>
    

    in styles.xml

    But this might induce a problem with your splashscreen (white screen at startup)... In this case, add the following line to your styles.xml:

     <item name="android:windowDisablePreview">true</item> 
    

    just below the windowIsTranslucent line.

    Last chance if the previous tips do not work : target SDK 26 instead o 27.

    0 讨论(0)
  • 2020-12-02 05:39

    I do not know if this is a bug from Google or an intended behavior but I (at least momentarily) solved it by changing compileSdkVersion and targetSdkVersion back to 26 in Gradle...

    0 讨论(0)
  • 2020-12-02 05:43

    In android Oreo (API 26) you can not change orientation for Activity that have below line(s) in style

     <item name="android:windowIsTranslucent">true</item>
    

    or

     <item name="android:windowIsFloating">true</item>
    

    You have several way to solving this :

    1) You can simply remove above line(s) (or turn it to false) and your app works fine.

    2) Or you can first remove below line from manifest for that activity

    android:screenOrientation="portrait"
    

    Then you must add this line to your activity (in onCreate())

        //android O fix bug orientation
        if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    

    3) You can create new styles.xml in values-v26 folder and add this to your style.xml. (Thanks to AbdelHady comment)

     <item name="android:windowIsTranslucent">false</item>
     <item name="android:windowIsFloating">false</item>
    
    0 讨论(0)
  • 2020-12-02 05:43

    I was getting this error when I try to capture image or take image from gallery what works for me is to remove both

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    and

    android:screenOrientation="portrait"
    

    now my activity is using this theme:

      <style name="Transparent" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:colorBackgroundCacheHint">@null</item>
            <item name="android:windowAnimationStyle">@android:style/Animation</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:backgroundDimEnabled">false</item>
        </style>
    
    0 讨论(0)
  • 2020-12-02 05:45

    Google throws this exception on Activity's onCreate method after v27, their meaning is : if an Activity is translucent or floating, its orientation should be relied on parent(background) Activity, can't make decision on itself.

    Even if you remove android:screenOrientation="portrait" from the floating or translucent Activity but fix orientation on its parent(background) Activity, it is still fixed by the parent, I have tested already.

    One special situation : if you make translucent on a launcher Activity, it has't parent(background), so always rotate with device. Want to fix it, you have to take another way to replace <item name="android:windowIsTranslucent">true</item> style.

    0 讨论(0)
提交回复
热议问题