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

Colton Idle

03/21/2023, 3:17 PM
What's the issue with passing a navigator into a composable or viewmodel?
p

Pablichjenkov

03/21/2023, 3:23 PM
In a Composable is not too bad, perhaps creates an extra dependency, one more to mock in tests, in the case of viewmodels might leak if not handled careful
j

jossiwolf

03/23/2023, 11:50 AM
A good part of an article I wrote a while ago talks about this: https://medium.com/google-developer-experts/navigating-in-jetpack-compose-78c78d365c6a
s

Stylianos Gakis

03/27/2023, 12:17 AM
Right, but this article doesn't mention anything regarding passing it to the ViewModel. Aside from making sure you want to keep your navigation logic in one place and not passing it down to every composable, what is the situation like when you want to pass it to the ViewModel in order to avoid having to make a trampoline move, of the VM telling the composable to navigate, which itself calls the NavController, and then have the composable report back to the ViewModel that yes this navigation happened. As opposed to skipping the middle man and having the ViewModel tell the NavController directly to navigate somewhere.
3 Views