Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock screen orientation when targeting Android API 27 with a non-opaque activity

I have an activity that has android:windowIsTranslucent set to true and android:windowBackground set to a translucent background. I just changed my target and compile sdk version to 27, and I get an exception when launching this activity now:

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

Since this is a new sdk, there isn't anything online about it yet (and it seems to result from this line of code: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/Activity.java#987 )

Is there any way to get around this? The app doesn't crash if I take out android:screenOrientation="portrait" from my manifest for this activity, but I would like to be able to keep it like that.

like image 552
vanshg Avatar asked Oct 27 '17 17:10

vanshg


3 Answers

I also faced the same problem. As others said, If I deleted android:screenOrientation="portrait" or overrided it with android:screenOrientation="unspecified", then the exception was gone. And it seems that the front activity's orientation follows the behind activity's orientation.

I thought about it. If the front activity is transparent and the behind activity's orientation is different, the display becomes strange. So, I can understand why this check logic was added However, I wonder that why this problem was not occurred in Developer Preview 8.0.0.

like image 52
Takao Sumitomo Avatar answered Nov 15 '22 18:11

Takao Sumitomo


The workaround is to set targetSdk back to 26.

The reason why is your application crashes is here in this commit.

As you can see here, you are not the only one - this behavior has been reported to Google as issue. It has been fixed, but we don't know how and when it will be released.


I can also confirm what "sofakingforever" says in comments, if there is non-translucent activity with fixed orientation behind your translucent, the translucent will not rotate. So you can just remove android:screenOrientation="portrait" from manifest as well.

like image 16
JerabekJakub Avatar answered Nov 15 '22 19:11

JerabekJakub


The solution worked for me is deleting

android:screenOrientation="portrait" 

from all the full screen transparent activities which means their theme contains

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

Also to make sure that orientation works correct for below Oreo I added this to the onCreate() of the activities.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This activity is a fullscreen transparent activity, so after Oreo Android doesn't allow fullscreen
    // transparent activities to specify android:screenOrientation="portrait" in the manifest. It will pick up
    // from the background activity. But for below Oreo we should make sure that requested orientation is portrait.
    if (VERSION.SDK_INT < VERSION_CODES.O) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
like image 7
omersem Avatar answered Nov 15 '22 17:11

omersem