如何使适当的splashscreen
在喷气背包组成?它可以工作,但首先它转到OnBoardingScreen
,然后它从ViewModel
加载数据,我为这些屏幕提供了相同的viewmodel
,并重新加载以从HomeScreen
开始。我想最好等到它从视图模型加载数据,然后选择屏幕。如何做到这一点?或者什么是更好的方法启动屏幕与喷气背包组成?
NavHost(
navController = navController,
startDestination = determineStartDestination(viewModel),
modifier = Modifier.fillMaxSize()
) {
private fun determineStartDestination(userModel: HomeViewModel): String {
return if (userModel.homeState.value.emailFrom.isNotEmpty() && userModel.homeState.value.emailTo.isNotEmpty()) {
OnHomeNav.getRoute()
} else {
OnBoardingScreenNav.getRoute()
}
}
编辑:它不工作
Change in android manifest:
android:theme="@style/Theme.MyApp.Starting"
MainActivity
installSplashScreen().apply {
setKeepOnScreenCondition {
homeViewModel.homeState.value.emailFrom.isNotEmpty() && homeViewModel.homeState.value.emailTo.isNotEmpty()
}
}
setContent { [...]
它启动这个新的启动画面,但逻辑与
setKeepOnScreenCondition { }
没什么用我有同样的行动。启动屏幕-> 1秒的注册屏幕->主屏幕
好吧,我想我已经找到了解决办法,但对我来说,这是不可接受的,因为我需要使用延迟。
init {
viewModelScope.launch {
getUser()
}
}
// before
private fun getUser() {
viewModelScope.launch {
userRepository.getUser().collectLatest { userModel ->
homeState.update {
copy(
userModel = userModel,
nameAndSurname = userModel?.nameAndSurname ?: "",
number = userModel?.phoneNumber ?: "",
email = userModel?.email ?: "",
registrationNumber = userModel?.registrationNumber ?: "",
emailTo = userModel?.emailTo ?: "",
emailFrom = userModel?.emailFrom ?: ""
)
}
}
}
}
// after
private fun getUser() {
viewModelScope.launch {
userRepository.getUser().filterNotNull().collectLatest { userModel ->
homeState.update {
copy(
userModel = userModel,
nameAndSurname = userModel?.nameAndSurname ?: "",
number = userModel?.phoneNumber ?: "",
email = userModel?.email ?: "",
registrationNumber = userModel?.registrationNumber ?: "",
emailTo = userModel?.emailTo ?: "",
emailFrom = userModel?.emailFrom ?: ""
)
}
delay(100L)
checkIfUserIsLoggedIn()
}
}
}
This is because homeState.update needs a little bit time to update date.
1条答案
按热度按时间eqqqjvef1#
我将向您展示我的实现,以便了解我是如何做到这一点的。它适用于我测试的任何Android版本。请记住,您应该在实际设备中看到它,而不是在Android Studio中,因为有时您会遇到问题!
执行情况
主要活动
然后在
viewModel
中应用您的逻辑并观察值查看模型