android 如何应用闪屏与jetpack组成

dojqjjoe  于 2023-09-28  发布在  Android
关注(0)|答案(1)|浏览(95)

如何使适当的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.
eqqqjvef

eqqqjvef1#

我将向您展示我的实现,以便了解我是如何做到这一点的。它适用于我测试的任何Android版本。请记住,您应该在实际设备中看到它,而不是在Android Studio中,因为有时您会遇到问题!

执行情况

//Splash Screen
implementation 'androidx.core:core-splashscreen:1.0.1'

主要活动

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
    .
    .
    .

        super.onCreate(savedInstanceState)

        vmProfile.checkIfUserIsLoggedIn()

        val content: View = findViewById(android.R.id.content)
        content.viewTreeObserver.addOnPreDrawListener(
            object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    return if (!vmProfile.isLoading.value) {
                        content.viewTreeObserver.removeOnPreDrawListener(this)
                        true
                    } else false
                }
            }
        )

        setContent {

                  .
                  .
                  .

然后在viewModel中应用您的逻辑并观察值

查看模型

private val _isLoading = MutableStateFlow(true)
val isLoading = _isLoading.asStateFlow()

fun checkIfUserIsLoggedIn() {
   .
   .
   .
   _isLoading.value = false
}

相关问题