Hello, I am using navigateUp() to go back to a previous screen. go from C To B. Sometimes it is goin...
e
Hello, I am using navigateUp() to go back to a previous screen. go from C To B. Sometimes it is going from C To A directly. Why it it is skipping screen B. Using navigation in jetpack compose
s
Did you check your backstack before doing this navigation, and made sure that it was A -> B -> C and pressing navigateUp made it be A ?
i
When you are on your task stack, up and back do the same thing, so that would be expected if system back also takes you to A: https://developer.android.com/guide/navigation/navigation-principles#up_and_back_are_identical_within_your_apps_task
e
It is calling navigateUp twice, I don't know why!
s
Well then it sounds like some logic error inside your code. If you shared how it’s called it may be something that’s obvious why it goes wrong
e
Copy code
CreateOrEditPatientEvent.CreatedPatient -> {

    viewModelScope.launch {
        delay(500)
        _state.value = _state.value.copy(
            isSaving = false
        )
        navController.navigateUp()
    }

}
that's my viewmodel
s
Well, if you add a log right before this, is that log also called twice? If yes, then this
CreatedPatient
event is called twice from your own code, so that’s an issue by itself, and has nothing to do with the navigation library. I’d start there 👀 As a side question, you got a navController instance inside your ViewModel I see, I wonder how exactly are you passing that it? Just curious.
e
passing it in the viewmodel class, and injecting it through koin
s
Right, using like
Copy code
koinViewModel { parametersOf(navController) }
I wonder if this has any implications with process death/configuration changes and if it retains any old reference since navController is created in composition, so if there’s a configuration change and an activity recreation (if you are not preventing that), a new one will be made. But it may be that it updates it properly with the new navController somehow, really not sure. Have you tested that yourself?
e
solved my issue using launched effect since I was sending my event many times ....
yes we use it like this, and my app is always on portrait mode. So I am not facing any issues till now
s
Configuration changes are much more than just switching portrait/landscape mode https://developer.android.com/guide/topics/resources/runtime-changes. Even changing dark/light mode will trigger one by default, among others. Have you noticed any issues in those cases?
e
even when switching dark/light theme nothing will change
s
Alright then 😊
e
how are you handling navigation in your app?
s
Like this https://developer.android.com/topic/architecture/ui-layer/events#handle-viewmodel-events by modeling events as state, (if the event originates in the VM) then triggering the thing (navigation in this case) and then clearing the state. It’s a ping-pong situation, not the most fun thing to do sometimes, but it does guarantee things only happening once, and never dropping an event. But I would prefer alternatives in some cases hence why I asked you here 😅 I want to explore other options too.
e
wha's your tech stack?
s
What do you mean?
e
what are you using in your current project
s
Uhm, I still don’t understand what you want to hear here. We’re using androidx.navigation for navigation, compose for UI, AAC ViewModels and follow a UDF structure of how ui interacts with the VM.