I'm utilizing the DataStore value isLanguageSelected to determine whether to display the HomeScreen() composable or the LanguageScreen(). While the functionality works as intended, I'm encountering an issue where there's a flickering effect when transitioning to the LanguageScreen(). For instance, if isLanguageSelected is true in DataStore, it briefly displays the LanguageScreen() composable before showing the HomeScreen(), resulting in the flickering effect. Here's the relevant code:
@Composable
fun MyAppNavHost(
navController: NavHostController = rememberNavController()
) {
val dataStore = LocalContext.current.dataStore
val userPreferencesRepository = UserPreferencesRepository(dataStore)
val isLanguageSelected by userPreferencesRepository.isLanguageSelected.collectAsState(initial = false)
NavHost(
navController = navController,
startDestination = if (!isLanguageSelected) LanguageScreen.route else HomeScreen.route
){
composable(HomeScreen.route) {
HomeScreen()
}
composable(LanguageScreen.route) {
LanguageScreen()
}
}
}
I've attempted a solution where I check if the DataStore is loaded first, but I'm not satisfied with the approach. I'm seeking a more professional alternative. Here's the code I tried:
var isDataLoaded by remember { mutableStateOf(false) }
LaunchedEffect(dataStore) {
delay(1000)
isDataLoaded = true
}
if (isDataLoaded)
NavHost(){}