Home > Back-end >  Check if first time using the app during launch
Check if first time using the app during launch

Time:03-23

I'm trying to implement a first time using screen (like any other app when you have to fill some options before using the app for the first time). I can't go to another Jetpack compose on an main activity on-create state because it check that every recomposition, and take me to the navigation path (I'd like to check the datastore entry once during launch), this what I already try, not seem to be working:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            val onBoardingStatus = dataStoreManager.onBoard.first()
            setContent {
                val navController = rememberNavController()
                OnBoardingNavHost(navController)
                navController.navigate(if (onBoardingStatus) "on_boarding" else "main") {
                    launchSingleTop = true
                    popUpTo(0)
                }
            }
        }
    }

it is possible to check that only once (in application class for example and not in oncreate?) please advice, thanks in advance

CodePudding user response:

You have to use LaunchedEffect for this, you can do something like this

enum class OnboardState {
    Loading,
    NoOnboarded,
    Onboarded,
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        var onboardingState by remember {
            mutableStateOf(OnboardState.Loading)
        }
        LaunchedEffect(Unit) {
            onboardingState = getOnboardingState()
        }
        when (onboardingState) {
            OnboardState.Loading -> showSpinner()
            OnboardState.NoOnboarded -> LaunchedEffect(onboardingState) {
                navigateToOnboarding()
            }
            OnboardState.Onboarded -> showContent()
        }
    }
}
  • Related