'Jetpack Compose Navigation calls navigate function repeatedly and freezes

Using a when function, I need to navigate to particular graph in my compose navigation tree. The issue is when navigating away from the current screen to the new one, the screen repeatedly flashes and freezes up. Checking the debugger, it shows me that the code just constantly jumps between LaunchedEffect and navController.navigate(ProDashboardScreens.ProDashboardGraph.route).

Can anyone see a problem in my set up?

    composable(BasicDashboardScreens.Manager.route) {

    val managerViewModel: ManagerViewModel = hiltViewModel()
    val managerState by managerViewModel.state.collectAsState()

    ManagerScreen(managerState, managerViewModel)

    LaunchedEffect(key1 = managerViewModel.navigationEvent) {

        managerViewModel.navigationEvent.collect {

            when (it) {

                AuthenticationScreens.Login -> {
                    navController.navigate(AuthenticationScreens.Login.route)
                }

                AuthenticationScreens.Register -> {
                    navController.navigate(AuthenticationScreens.Register.route)
                }

                ProDashboardScreens.ProDashboardGraph -> {
                    navController.navigate(ProDashboardScreens.ProDashboardGraph.route)
                }

                else -> Unit
            }
        }
    }
}

  when  {

    !state.userAuthenticated -> {
        UnauthenticatedScreen(
            title = {
                Text(
                    text = stringResource(id = R.string.login_or_register),
                    style = MaterialTheme.typography.subtitle1.copy(
                        fontWeight = FontWeight.Bold
                    ),
                    textAlign = TextAlign.Center
                )
            },
            buttonOne = {
                AppButton(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(64.dp),
                    buttonText = stringResource(id = R.string.login),
                    buttonColor = Color.Blue,
                    enabled = true,
                    onClick = {
                        events.loginClicked()
                    }
                )
            },
            buttonTwo = {
                AppButton(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(64.dp),
                    buttonText = stringResource(id = R.string.register),
                    buttonColor = Color.Blue,
                    enabled = true,
                    onClick = {
                        events.registerClicked()
                    }
                )
            }
        )
    }

    state.userAuthenticated && !state.userProfile.proVersion -> {
        UnauthenticatedScreen(
            title = {
                Text(
                    text = stringResource(id = R.string.purchase_pro_enable_access),
                    style = MaterialTheme.typography.subtitle1.copy(
                        fontWeight = FontWeight.Bold
                    ),
                    textAlign = TextAlign.Center
                )
            },
            buttonOne = {
                AppButton(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(64.dp),
                    buttonText = stringResource(id = R.string.purchase_pro),
                    buttonColor = Color.Blue,
                    enabled = true,
                    onClick = {
                        events.purchaseProClicked()
                    }
                )
            }
        )
    }

    else -> {
        events.navigateToProGraph()
    }

}

@HiltViewModel
class ManagerViewModel @Inject constructor(
private val userProfileDao: UserProfileDao,
private val authManager: AuthManager
) : ViewModel(), ManagerEvents {

private val _state = MutableStateFlow(ManagerState.defaultState)
val state: StateFlow<ManagerState> = _state

private val _navigationEvent = MutableSharedFlow<NavigationRoute>()
val navigationEvent: SharedFlow<NavigationRoute> = _navigationEvent

init {
    viewModelScope.launch {
        _state.value = state.value.copy(
            userProfile = userProfileDao.getUserProfile().first() ?: UserProfile.defaultState,
            userAuthenticated = authManager.isAuthenticated()
        )
    }
}

override fun loginClicked() {
    viewModelScope.launch {
        _navigationEvent.emit(AuthenticationScreens.Login)
    }
}

override fun registerClicked() {
    viewModelScope.launch {
        _navigationEvent.emit(AuthenticationScreens.Register)
    }
}

override fun navigateToProGraph() {
    viewModelScope.launch {
        _navigationEvent.emit(ProDashboardScreens.ProDashboardGraph)
    }
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source