繁体   English   中英

当用户第二次使用 Jetpack Compose with Flow 打开应用程序时,跳过 NavHost 中的登录页面

[英]Skip landing page in NavHost when user opens the app for second time using Jetpack Compose with Flow

我在 startDestination 屏幕上有一个带有HorizontalPager ntalPager 的应用程序,在我 go 到最后一页后,该应用程序显示主页。 当我第二次打开该应用程序时,它应该立即显示主页,并且不再显示带有HorizontalPager ntalPager 的 startDestination 屏幕。 我使用了dataStore,它可以工作,但问题是每次我打开应用程序时,Horizo HorizontalPager登陆页面都会闪烁一秒钟,然后切换到主页。 我使用流来获取应用程序启动的真/假 state,所以它会知道它会知道应用程序已经第一次打开。

class MainActivity : ComponentActivity() {
    @ExperimentalAnimationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            WebSafeTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    val navController = rememberNavController()
                    Navigation(navController)
                }
            }
        }
    }
}

@ExperimentalAnimationApi
@ExperimentalMaterialApi
@ExperimentalFoundationApi
@ExperimentalPagerApi
@Composable
fun Navigation(
    navController: NavHostController
    ) {
    val context = LocalContext.current
    val preferencesManager = PreferencesManager(context)
    val preferencesFlow = preferencesManager.preferencesFlow
    val scope = rememberCoroutineScope()
    val result = remember { mutableStateOf(Constants.SKIP_LANDING_PAGE) }
    scope.launch {
        result.value = preferencesFlow.first().skipLandingPage
    }

    NavHost(
        navController = navController,
//it goes it this line 2 times, first time when the app opens and second time after the flow is finished
        startDestination = if (result.value) Screen.HomeScreen.route else Screen.LandingScreen.route,
        modifier = Modifier.fillMaxSize()
    ) {
        composable(
            route = Screen.LandingScreen.route
        ) {
            Landing(navController)
        }
        composable(
            route = Screen.SkipChooseCountryScreen.route
        ) {
            ChooseCountry()
        }
        composable(
            route = Screen.HomeScreen.route
        ) {
            Home(navController)
        }
    }
}

它在应用程序打开后第一次进入 NavHost,它总是返回 FALSE,因为它是默认值,在该流程返回 TRUE 之后(因此它知道应用程序之前至少打开过一次),然后它打开正确的屏幕。

我不知道如何让 NavHost 等待该流程完成。 我试图将 NavHost 放入 scope 但它不允许我。

我还需要从数据存储中读取数据,所以我是这样做的:

Crossfade(
    targetState = state.value.loadState
) { loadState ->
    when (loadState) {
        LoadState.NOT_LOADED -> Box(modifier = Modifier.fillMaxSize())
        LoadState.SHOW_PIN -> PinScreen(
            loadState = loadState,
            state = state,
            modifier = Modifier.fillMaxSize(),
        )
        LoadState.SHOW_CONTENT -> MainContent(
            state = state,
        )
    }
}

最初我的 state 是NOT_LOADED所以我只显示一个填满屏幕的空框。 您也可以显示微调器或应用程序的徽标。 加载数据后,如果用户启用了 PIN,我将显示 PIN 屏幕,否则显示主屏幕。

另外,请注意,您不应该将您的浏览器屏幕作为您的根目的地,您的主页应该是您的根目的地,并且如果用户还没有看到您的入职流程,您应该有条件地导航到浏览器。 检查以获取详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM