Hi, people!
I'm trying to create a new app following Clean Architecture to the best of my abilities.
However, I am unable to pass lambdas to my
ViewModel
's constructor (as it is constructed using
hiltViewModel()
).
I read a bit and found that NavArguments and SavedStateHandle should be used to pass arguments in ViewModel, ideally.
Yet, that only seems to support very limited
NavArg.Type
's as well. I don't get what I am doing wrong.
Are we not supposed to pass navigation lambdas to ViewModel? Because the state that needs to trigger navigation resides inside the ViewModel only.
Please give your helpful inputs :)
Thread in Slack Conversation
d
dewildte
05/09/2023, 5:54 PM
First of all, navigation is a UI concern.
In my experience handling navigation inside the ViewModel is not scalable
and arguably not a responsibility of the ViewModel.
The ViewModel is a state holder.
Keep navigation in Composables, Fragments, and Activities and it will scale nicely.
dewildte
05/09/2023, 5:56 PM
Secondly, a ViewModel Factory is the object that is used to pass arguments into
the constructor if you are not using Hilt to do dependency injection.
Also I would not pass a lamda into the constructor of the ViewModel.
Stick with Interfaces or concrete classes.
a
Advitiay Anand
05/10/2023, 2:13 AM
I get it. I read an SO answer that told to use LaunchEffect with a key and trigger navigation inside its block. Guess I'll try that and see how it goes.