按下电源按钮后,Android 12 SplashScreen API 显示黑色

标签 android android-splashscreen

更新
如果我的手机有锁屏,这个问题就不会发生
我正在使用 "androidx.core:core-splashscreen:1.0.0-alpha01" . SplashScreen 显示良好(带有 ic_launcher 图标的白色背景),但存在问题。
SplashScreen 显示后,我按电源键(手机屏幕关闭),然后我再次按电源键(手机屏幕打开,状态栏显示),但 SplashScreen 显示黑屏。这是我的演示代码
主题.xml

<style name="Theme.MySplash" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/white</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_background</item>
    <item name="postSplashScreenTheme">@style/Theme.AndroidSplashScreen12</item>
</style>

<style name="Theme.AndroidSplashScreen12" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>
AndroidManifest
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AndroidSplashScreen12">

    <activity android:name=".SplashActivity"
        android:exported="true"
        android:theme="@style/Theme.MySplash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
飞溅 Activity
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val splashScreen = installSplashScreen()
        setContentView(R.layout.activity_splash)

        val content: View = findViewById(android.R.id.content)
        content.viewTreeObserver.addOnPreDrawListener { false } // keep SplashScreen display until move to MainActivity
 
        // I will do some long operator here then move to MainActivity
    }
}

最佳答案

在 alpha 版本中出现错误是正常的。我尝试了相同的情况,有时屏幕变黑。我使用此代码暂停启动导航并且黑屏没有再次发生

SplashScreen.KeepOnScreenCondition {
    !renderMain
}

关于按下电源按钮后,Android 12 SplashScreen API 显示黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69159001/

相关文章:

java - appium如何从图库中选择照片

android - 软键盘隐藏编辑文本

android - DisplayMessageActivity.java 上的 MyFirstApp android 教程错误

java.lang.RuntimeException : Unable to start activity ComponentInfo{. .}java.lang.NullPointerException

android - 如何创建可重用的 UI 组件

android - SplashScreen API 有时不显示图标

android - 从 Android 12 中删除默认启动画面(示例)

Android:启动画面分辨率