Android 13通过Widget启动应用时Splash Screen图标不显示
解决Widget启动时SplashScreen图标不显示的问题
问题原因
从Widget启动应用时,触发的Intent未包含ACTION_MAIN和CATEGORY_LAUNCHER标记,core-splashscreen库默认仅对带LAUNCHER类别的主入口启动Intent完整展示SplashScreen图标;直接启动Activity的Intent会被判定为非主入口启动,导致图标无法加载。
解决方案
1. 修正Widget的Intent配置
在创建启动Activity的Intent时,添加主入口的Action和Category,模拟正常启动的Intent结构:
val notificationIntent = Intent(context, AuthenticationActivity::class.java).apply { // 添加主入口标记 action = Intent.ACTION_MAIN addCategory(Intent.CATEGORY_LAUNCHER) // 保留原有flags,补充FLAG_ACTIVITY_NEW_TASK适配跨进程启动 flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK }
2. 调整PendingIntent创建方式
仅启动单个Activity时,无需使用getActivities,改用getActivity更适配场景,同时适配Android 12+的PendingIntent权限要求:
// 根据Android版本选择合适的flags,Android 12+推荐使用FLAG_IMMUTABLE val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { PendingIntent.FLAG_IMMUTABLE } else { PendingIntent.FLAG_UPDATE_CURRENT } val notificationPendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, flags)
3. 确认SplashScreen主题配置
当前主题配置已符合要求,可补充动画时长确保图标展示逻辑正常:
<style name="Theme.MyApp.Start" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@color/color_primary</item> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_splash_foreground</item> <item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_launcher_splash_foreground</item> <item name="postSplashScreenTheme">@style/Theme.MyApp.PreLogin</item> <item name="android:windowSplashScreenBehavior">icon_preferred</item> <!-- 可选:设置图标动画时长,确保展示逻辑稳定 --> <item name="windowSplashScreenAnimationDuration">300</item> </style>
额外验证点
- 确认
AuthenticationActivity中installSplashScreen()调用顺序正确(必须在super.onCreate(savedInstanceState)之前),当前代码已符合要求; - 检查图标资源是否覆盖所有分辨率目录,避免因资源缺失导致不显示。
内容的提问来源于stack exchange,提问作者Hakan AKKAYA
相关产品推荐
相关产品推荐

