如何保持 SplashScreen 显示直到 dataStore 加载到 jetpack compose 中?

问题描述 投票:0回答:1

我想保留 SpalshScreen (SplashScreen Api) 直到加载数据存储。加载数据存储后,它应该转到主应用程序内容。这是我尝试过的代码,但它没有按预期工作。

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)
        setContent {

            val dataStore = this.dataStore
            var isLoading by remember {
                mutableStateOf(false)
            }

            LaunchedEffect(dataStore) {
                dataStore.data.collect{
                    isLoading = true
                }
            }
            splashScreen.setKeepOnScreenCondition{ isLoading }

            MyAppTheme{
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background,
                ) {
                    MyApp()
                }
            }
        }
    }
}

这是我的启动画面主题:

<resources>
        <style name="Theme.Kanago.Starting" parent="Theme.SplashScreen">
            <item name="windowSplashScreenBackground">#FFFFFF</item>
            <item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen_icon</item>
            <item name="windowSplashScreenAnimationDuration">200</item>
            <item name="postSplashScreenTheme">@style/Theme.Kanago</item>
        </style>
</resources>
android-jetpack-compose android-jetpack android-splashscreen
1个回答
0
投票

您正在将

isLoading
变量初始化为
false
。所以你的 SplashScreen 正在监听条件

splashScreen.setKeepOnScreenCondition{ isLoading }

可能会立即隐藏,我不确定当你将

isLoading
设置为
true
时它是否会再次显示。

首先,尝试将

isLoading
初始化为
true
,看看是否已经解决了您的问题:

var isLoading by remember {
    mutableStateOf(true)
}

如果没有,您需要重新设计逻辑,在数据准备好后将

isLoading
设置为
false
,然后 SplashScreen 将被隐藏。
很抱歉,我现在无法提供更多详细信息,因为我不太明白您的代码在做什么。

© www.soinside.com 2019 - 2024. All rights reserved.