https://kotlinlang.org logo
#compose
Title
# compose
a

Android75

04/05/2022, 4:11 AM
I don’ understand how can use navController and change scree/pager. I have 3 screen @Ian Lake can you help me. I need to change page and page is decide by viewModel ``````
i

Ian Lake

04/05/2022, 4:15 AM
Put your code in a thread as per the pinned message: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1616265877303000
a

Android75

04/05/2022, 4:24 AM
Copy code
@Composable
fun NavigationComponent(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = Destination.Splash.path
    ) {
        composable(Destination.Splash.path) {
            SplashScreen(navController)
        }
        composable(Destination.Dashboard.path) {
          
            DashboardScreen(navController)
        }
        composable(Destination.SectionPage.path+"/{type}") {backStackEntry ->
            SectionPage(navController, backStackEntry.arguments?.getString("type"))

        }
    }
}
Every screen receives navController and has its ViewModel. ViewModel decides destination, of course.
Copy code
@Composable
fun SplashScreen(navController: NavHostController) {
    val viewModel = hiltViewModel<SplashViewModel>()
     var splashScreenState = viewModel.uiState.collectAsState().value
ViewModel uses stateUi to change screen.
Copy code
@HiltViewModel
class SplashViewModel @Inject constructor() : BaseViewModel() {
    val uiState = MutableStateFlow(SplashState())
    init {
        Handler(Looper.myLooper()!!).postDelayed({
            gotoDashBoard()
        }, 2000)
    }

    private fun gotoDashBoard() {

      uiSate.value = uiState.value.copy(
            navigate = Navigate(true, destination= NavigationItem.Dashboard)
        )
    }
}
In SplashSreen :
Copy code
if(splashScreenState.navigate.change){
    navController.navigate(splashScreenState.navigate.destination.route)
}
ok.. i have a loop. I see Dashboard screen is recreated continuously. I put this and works.. but i don’t like it
Copy code
if(splashScreenState.navigate.change){ 
    splashScreenState.navigate.change= false
    navController.navigate(splashScreenState.navigate.destination.route)
}
I’d like follow MVVM pattern, ViewModel decides and sends to view state to change “page”
i

Ian Lake

04/05/2022, 4:30 AM
You really didn't follow either of the other things I said
Don't use Navigation for your splash screen - use the Splash screen APIs: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1649050716032649?thread_ts=1649039860.384509&amp;cid=CJLTWPH7S
a

Android75

04/05/2022, 4:42 AM
ok viewModel doesn’t have a navController instance, i can’t use
navigate
inside composition so.. how and where i can put code to change screen? Ok for splashscreen but if in Dashboard has 3 buttons.. if i want follow MVVM, buttons send event to ViewModel and after ViewModel decides destination.. how can i change destination..
i

ildar.i [Android]

04/05/2022, 5:15 AM
make a event SharedFlow, post events there from ViewModel, subscribe to it in LaunchedEffect and you’re good to go - you can navigate anywhere from LaunchedEffect
a

Android75

04/05/2022, 5:40 AM
as this code
Copy code
LaunchedEffect(Unit) {
    viewModel.someUIState.collect { uiState -> 
        when(uiState) {
            // Same as in the question
        }
    }
}
ok it works but when from Page2 use a navController.popBackStack() Page1 has the State with navigation to Page2 so i can’t back
Copy code
LaunchedEffect(uiState) {

        if(uiState.destination.isNotEmpty()) {
            navController.navigate(uiState.destination)
        }

}

DisposableEffect(LocalLifecycleOwner.current) {
    onDispose {
        viewModel.resetState()
    }
}
i

ildar.i [Android]

04/05/2022, 7:57 AM
Use SharedFlow instead of StateFlow
g

gildor

04/05/2022, 9:39 AM
@Android75 A side note about your message. I think it’s not nice to ask in a public channel and mention particular person to help you the same time. It’s not twitter. It may be inconvenient for this person to be essentially obligated to answer you and it also discourages other to give you advice It’s my personal opinion, not like a moderator
i

ildar.i [Android]

04/05/2022, 9:43 AM
While I agree with you, but as a person with two ignored questions I can undestand him)
g

gildor

04/05/2022, 9:44 AM
Well there is probably a reason for this. It’s not a free service of support, nobody obligated to answer you, but it puts those “obligations” to answer if you ping someone specific
👍 1
a

Android75

04/05/2022, 10:03 AM
Sorry, but i ping Ian lake because yiou help me last conversation about navigation.. i apologize
7 Views