【问题标题】:Android 12 SplashScreen API display black after press power button按下电源按钮后,Android 12 SplashScreen API 显示黑色
【发布时间】:2021-09-13 07:45:26
【问题描述】:

更新
如果我的手机有锁屏,就不会出现这个问题

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

themes.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>

SplashActivity

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
    }
}

【问题讨论】:

  • 我认为闪屏库的想法是避免使用闪屏活动,你不能在 MainActivity 中进行长时间运行的操作,然后隐藏闪屏吗?
  • @ScottCooper 我当前的 SplashScreen 活动内部有许多逻辑和路由,因此很难将其移动到 MainActivity。 developer.android.com/about/versions/12/… 来自迁移文档,有一个保持当前 SplashActivity 的选项

标签: android android-splashscreen


【解决方案1】:

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

SplashScreen.KeepOnScreenCondition {
    !renderMain
}

【讨论】:

  • 谢谢。我尝试了 KeepOnScreenCondition,但在我的手机上出现了同样的问题(Pixel 4XL Android 12,最后一次更新是 9 月 5 日)。同意你的看法,这可能是 alpha 版本中的一个错误,我们现在无法采取任何措施来防止它
  • 我发现了另一件事。如果我的手机有锁屏,则不会发生此问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多