Updated to 4.2RC1 and transparent activities do not work ? Suddenly black
Asked Answered
C

1

3

I have a sidemenu activity which appears over another activity.

<style name="Sidemenu" parent="Usual">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsTranslucent">true</item>

    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

It has been working perfectly. As normal, you can see the activity below.

However I simply updated Android Studio to 4.2RC1 ..

and now .. it does not work!

The under area is black.

Total mystery.

Any solutions around?

Crenel answered 11/4, 2021 at 3:29 Comment(0)
C
18

courtesy this amazing old post where there was a similar problem a few years ago: https://mcmap.net/q/110260/-how-to-programatically-theme-an-activity-to-be-like-a-dialog

4.2RC1 Workaround:

(1) It's yet another droid activity/theme bug

(2) In manifest you MUST set the theme to Theme.AppCompat.Dialog

    // side menu
    <activity android:name=".. LeftMenu" android:theme="@style/Theme.AppCompat.Dialog" />
    <!-- beware of insane droid transparent activity bug... -->

(3) Only in code, can you set the theme to your theme. (Do so before super)

protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.YourTransparentTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aty_leftmenu);
}

(4) Your own theme in styles will be:

<style name="YourTransparentTheme" parent="YourGeneralTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    .. and other colors etc you desire ..
</style>

Details on why you only need windowBackground: https://mcmap.net/q/53180/-how-do-i-create-a-transparent-activity-on-android

In summary the workaround for the bug is:

Must set Theme.AppCompat.Dialog in manifest. You may only set your own style in onCreate.

(Aside: Note that it is no problem if most of your activities have a title bar (so based on Theme.AppCompat.Light), but your transparent activity has no title bar (so based on Theme.AppCompat.Light.NoActionBar). In that way you can have the usual thing where a "left menu" or similar covers also the title bar of the main app.)

Crenel answered 11/4, 2021 at 12:46 Comment(4)
I have this bug with 4.1.3 stable and this solution helped to work around the bug.Jabez
This helped me figure out a workaround too.Handle
wanted to report that this also worked for me when nothing else did (I was just getting a black screen when trying to show an Activity with transparent background).. and I'm running OS:11 on a Pixel 3aPilotage
Just wow! Thank you very much for this! My activity stopped becoming transparent when I updated the librariesCoordination

© 2022 - 2024 — McMap. All rights reserved.