Parcourir la source

修复启动时的闪烁问题 #78

HapeLee il y a 1 an
Parent
commit
bbceb80f5c

+ 2 - 0
app/build.gradle

@@ -331,4 +331,6 @@ dependencies {
 
     implementation libs.androidx.datastore.preferences
     implementation libs.androidx.palette
+    implementation libs.androidx.core.splashscreen
+    implementation libs.androidx.startup.runtime
 }

+ 2 - 2
app/src/main/AndroidManifest.xml

@@ -125,8 +125,8 @@
         </activity-alias>
         <activity
             android:name=".ui.welcome.SplashActivity"
-            android:alwaysRetainTaskState="true"
-            android:enableOnBackInvokedCallback="true"
+            android:launchMode="singleTop"
+            android:theme="@style/Theme.App.Starting"
             android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

+ 24 - 35
app/src/main/java/io/legato/kazusa/ui/welcome/SplashActivity.kt

@@ -3,11 +3,8 @@ package io.legato.kazusa.ui.welcome
 import android.annotation.SuppressLint
 import android.content.Intent
 import android.os.Bundle
-import android.transition.Fade
 import androidx.core.app.ActivityOptionsCompat
-import androidx.core.content.ContextCompat.startActivity
-import androidx.core.view.postDelayed
-import com.google.android.material.transition.platform.MaterialSharedAxis
+import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
 import io.legato.kazusa.base.BaseActivity
 import io.legato.kazusa.constant.PreferKey
 import io.legato.kazusa.databinding.ActivitySplashBinding
@@ -22,40 +19,32 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>() {
     override val binding by viewBinding(ActivitySplashBinding::inflate)
 
     override fun onCreate(savedInstanceState: Bundle?) {
+        val splashScreen = installSplashScreen()
         super.onCreate(savedInstanceState)
+        splashScreen.setKeepOnScreenCondition { true }
+        routeNext()
+    }
 
-        window.exitTransition = Fade().apply {
-            duration = 300
-        }
-        window.reenterTransition = Fade().apply {
-            duration = 300
-        }
-
-        if (LocalConfig.isFirstOpenApp) {
-            startActivity(
-                Intent(this, WelcomeActivity::class.java),
-                ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle()
-            )
-            finish()
-            return
-        }
-
-        if (getPrefBoolean(PreferKey.defaultToRead)) {
-            startActivity(
-                Intent(this, MainActivity::class.java),
-                ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle()
-            )
-            startActivity(
-                Intent(this, ReadBookActivity::class.java),
-                ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle()
-            )
-            finish()
-            return
+    private fun routeNext() {
+        when {
+            LocalConfig.isFirstOpenApp -> {
+                go(WelcomeActivity::class.java)
+            }
+            getPrefBoolean(PreferKey.defaultToRead) -> {
+                go(MainActivity::class.java)
+                go(ReadBookActivity::class.java)
+            }
+            else -> {
+                go(MainActivity::class.java)
+            }
         }
-
-        startActivity(Intent(this, MainActivity::class.java),
-            ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle())
         finish()
     }
-}
 
+    private fun go(target: Class<*>) {
+        startActivity(
+            Intent(this, target),
+            ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle()
+        )
+    }
+}

+ 4 - 2
app/src/main/res/layout/activity_splash.xml

@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.constraintlayout.widget.ConstraintLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:background="?attr/colorSurface">
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 6 - 0
app/src/main/res/values/themes.xml

@@ -13,6 +13,12 @@
         <item name="bottomNavigationStyle">@style/Widget.Material3Expressive.BottomNavigationView</item>
     </style>
 
+    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
+        <item name="windowSplashScreenBackground">?attr/colorSurface</item>
+        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
+        <item name="postSplashScreenTheme">@style/Base.AppTheme</item>
+    </style>
+
     <style name="ThemeOverlay.WhiteBackground" parent="">
         <item name="colorOnPrimary">@color/wh_theme_onPrimary</item>
         <item name="colorOnSecondary">@color/wh_theme_onSecondary</item>

+ 4 - 0
gradle/libs.versions.toml

@@ -1,5 +1,6 @@
 [versions]
 
+coreSplashscreen = "1.0.0-beta02"
 datastorePreferences = "1.1.7"
 kotlin = "2.2.20"
 ksp = "2.2.10-2.0.2"
@@ -44,6 +45,7 @@ desugar = "2.1.5"
 
 activity = "1.11.0"
 #kotlinxSerialization = "1.8.0"
+startupRuntime = "1.1.1"
 swiperefreshlayout = "1.1.0"
 #noinspection GradleDependency
 recyclerview = "1.4.0"
@@ -65,8 +67,10 @@ androidx-annotation = { group = "androidx.annotation", name = "annotation", vers
 #androidx-annotation-experimental = { group = "androidx.annotation", name = "annotation-experimental", version = "1.3.1" }
 
 androidx-collection = { module = "androidx.collection:collection", version.ref = "collection" }
+androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "coreSplashscreen" }
 androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
 androidx-palette = { module = "androidx.palette:palette", version.ref = "palette" }
+androidx-startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "startupRuntime" }
 appcompat-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
 androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }