Android:Splash Screen API的postSplashScreenTheme动态主题不生效问题
Splash Screen API闪屏后启动Activity无法应用Material You动态颜色
按照Android官方文档实现Splash Screen API后,闪屏结束后的启动Activity无法像应用其他部分一样使用动态颜色主题。已在闪屏主题中设置<item name="postSplashScreenTheme">@style/Theme.App</item>,理论上闪屏结束后会将基础应用主题应用到启动Activity,但实际并未生效。所有实现均严格遵循文档要求(Activity类、清单文件、themes.xml等)。
尝试过所有可能的组合,唯一“可行”的方式是不在清单中为初始Activity设置闪屏主题(即android:theme="@style/Theme.App.Starting"),但文档明确要求必须这么做,想知道原因及解决办法。
相关代码
闪屏主题(themes.xml)
<!-- Splash Screen Theme. --> <style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">?android:attr/colorBackground</item> <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item> <item name="windowSplashScreenAnimationDuration">200</item> <item name="postSplashScreenTheme">@style/Theme.App</item> <!-- Status bar and Nav bar configs --> <item name="android:statusBarColor" tools:targetApi="l">?android:attr/colorBackground</item> <item name="android:navigationBarColor">?android:attr/colorBackground</item> <item name="android:windowLightStatusBar">true</item> </style>
清单文件(AndroidManifest.xml,仅展示相关属性)
<application android:name=".application.App" android:theme="@style/Theme.App" tools:targetApi="31"> <activity android:name=".ui.MainActivity" android:theme="@style/Theme.App.Starting"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
MainActivity代码
override fun onCreate(savedInstanceState: Bundle?) { setupSplashScreen() super.onCreate(savedInstanceState) /*...*/ }
Application类代码
class App : Application() { override fun onCreate() { super.onCreate() // Apply dynamic color DynamicColors.applyToActivitiesIfAvailable(this) } }
2023年7月5日更新(最终结论)
经过大量测试和研究,确认这是一个短期内无法修复的官方Bug,该Bug在一年半前就已被上报。问题根源在于Splash Screen库在调用postSplashScreenTheme后会重新设置应用基础主题,从而清除Material You(动态)颜色配置。
目前已知的临时方案均存在缺陷:
- 有人建议在每个Activity的
setupSplashScreen()后调用DynamicColors.applyToActivitiesIfAvailable(this),但测试后发现状态栏仍会保留基础主题颜色,并无效果。 - 不在清单中为初始Activity设置闪屏主题的方式仅在API 31+上可行,但在更低版本中闪屏图标完全不显示,无法兼容多版本。
综上,当前Splash Screen API与Material You动态颜色存在冲突,二者互斥。由于需要使用闪屏API在启动时加载数据,最终决定放弃使用动态颜色。
内容的提问来源于stack exchange,提问作者Synth
相关产品推荐
相关产品推荐

