Android适配最新SplashScreen方案

某天我照常开发新应用时,在我的闪屏页SplashActivity中发现一个warning提示:The application should not provide its own launch screen。

于是查阅了相关资料发现,google已不再建议SplashActivity+MainActivity这样的启动流程了,而是更改为只有一个MainActivity,在MainActivity中先展示闪屏页面,待加载完毕后展示主页面

使用方法

首先添加splash screen依赖

scss 复制代码
dependencies {
    implementation("androidx.core:core-splashscreen:1.0.1")
}

接下来要定义一个Splash Theme主题,在styles.xml中添加定义

ini 复制代码
<style name="Theme.Splash" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/background</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
    <item name="android:windowBackground">@color/background</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowSplashScreenBrandingImage">@drawable/...</item> 
   <item name="postSplashScreenTheme">@style/Theme.YourTheme</item>
</style>

其中,postSplashScreenTheme需要替换为后续的Activity主题,这个主题将在闪屏页结束后生效

在app的Manifest文件中把MainActivity的主题声明为该Splash主题

ini 复制代码
<activity
    android:name=".ui.MainActivity"
    android:exported="true"
    android:theme="@style/Theme.Splash" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

最后,在MainActivity中,需要在onCreate方法中调用installSplashScreen方法,这个方法应该尽早调用,以更早地显示闪屏内容。如果需要在展示首页内容前进行一些加载,可以通过splash.setKeepOnScreenCondition方法设置条件

kotlin 复制代码
override fun onCreate(savedInstanceState: Bundle?) {
    val splash = installSplashScreen()
    super.onCreate(savedInstanceState)
    var isLoading by mutableStateOf(true)
    lifecycleScope.launch {
        // ...
        isLoading = false
    }
    splash.setKeepOnScreenCondition { isLoading }
    setContent {
        // ...
    }
}

设置完成后,启动应用就可以得到闪屏效果了

兼容问题

虽然google宣称SplashScreen API是支持Android11及以前版本的,但是在测试中仍发现了部分机型(如moto)的闪屏效果不生效。为了解决该兼容问题,定义了两套Splash的主题,Android12以上版本使用新的Splash Theme,放在v31/styles.xml中,旧版本则使用传统的Theme,定义如下

ini 复制代码
<style name="Theme.Splash" parent="YourAppTheme">
    <item name="android:windowBackground">@drawable/splash_background</item>
    <item name="android:windowFullscreen">true</item>
</style>

其中splash_backgroud.xml定义为

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background" />
    <item
        android:drawable="@drawable/ic_splash"
        android:gravity="center" />
</layer-list>
相关推荐
一笑的小酒馆5 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺7 小时前
Android BLE 扫描完整实战
android
TeleostNaCl9 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952710 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_9159184111 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong95111 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海11 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿11 小时前
毕业三年后,我离职了
android·面试
编程乐学12 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐13 小时前
Android14 init.rc中on boot阶段操作4
android