DEV Community

Discussion on: Handling back presses in Jetpack Compose

Collapse
 
rys07 profile image
rys07

Great article, thanks!

I am just curious. How do you implement navigation inside the view model (onAction)? Does it have a dependency to the navigation controller?

Collapse
 
pawegio profile image
Paweł Gajda

Glad you liked it. I tend to delegate the navigation to the navigator implementation (hidden behind the interface). It obtains a reference to the NavHostController when it's created for the NavHost.

Collapse
 
rys07 profile image
rys07

Good idea. The only problem I see here is that view model survives configuration change and has a reference to the old navigator when a new navigation controller is created in Composable. It can be replaced in view model, however, it could be tricky when using DI.