You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Android应用从App Shortcut启动时显示自定义Splash Screen?

解决App Shortcuts跳转时不显示自定义Splash Screen的问题

问题核心在于:应用快捷方式(App Shortcuts)指向的目标Activity未接入core-splashscreen的初始化流程,导致系统 fallback 到默认启动屏。以下是具体解决步骤:

1. 为所有入口Activity配置SplashScreen主题

确保应用中所有可能被外部(包括快捷方式)直接启动的Activity,都在Manifest中设置SplashScreen主题。

首先在res/values/styles.xml中定义你的SplashScreen主题(若未定义):

<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">
    <!-- 自定义启动屏背景 -->
    <item name="windowSplashScreenBackground">@color/splash_background</item>
    <!-- 自定义动画图标(支持静态或AnimatedVectorDrawable) -->
    <item name="windowSplashScreenAnimatedIcon">@drawable/animated_splash_icon</item>
    <!-- 动画持续时间(毫秒) -->
    <item name="windowSplashScreenAnimationDuration">2000</item>
    <!-- 启动完成后切换到的应用主主题 -->
    <item name="postSplashScreenTheme">@style/Theme.App.Main</item>
</style>

然后在AndroidManifest.xml中,给快捷方式对应的目标Activity设置该主题:

<activity
    android:name=".ShortcutTargetActivity"
    android:theme="@style/Theme.App.SplashScreen"
    android:exported="true">
    <!-- 快捷方式的Intent Filter或其他配置 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

2. 在每个入口Activity的onCreate中初始化SplashScreen

必须在super.onCreate(savedInstanceState)之前调用installSplashScreen()方法,确保启动屏的动画逻辑被触发。示例代码(Kotlin):

class ShortcutTargetActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // 初始化SplashScreen,必须在super.onCreate之前执行
        val splashScreen = installSplashScreen()
        
        // 若需等待数据加载完成再隐藏启动屏,可设置保留条件
        splashScreen.setKeepOnScreenCondition {
            // 替换为你的数据加载状态标记,比如isDataLoading
            false
        }
        
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_shortcut_target)
    }
}

3. 检查快捷方式的Intent配置

确保快捷方式的Intent没有添加会干扰启动屏的标记,比如FLAG_ACTIVITY_NO_ANIMATION或FLAG_ACTIVITY_CLEAR_TOP等可能跳过启动流程的flag。

示例快捷方式配置(res/xml/shortcuts.xml):

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="sample_shortcut"
        android:enabled="true"
        android:icon="@drawable/shortcut_icon"
        android:shortcutShortLabel="@string/shortcut_short_label"
        android:shortcutLongLabel="@string/shortcut_long_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.your.app.package"
            android:targetClass="com.your.app.package.ShortcutTargetActivity" />
        <!-- 避免添加可能阻止启动屏的intent flags -->
    </shortcut>
</shortcuts>

4. 适配Android 12+的系统行为

对于Android 12(API 31)及以上设备,系统会优先识别应用的SplashScreen主题配置,只要上述步骤正确执行,就能覆盖快捷方式启动时的默认启动屏行为。


内容的提问来源于stack exchange,提问作者Hector

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:30:48