Hey everyone, A quick question regarding ViewMod...
# dagger
b
Hey everyone, A quick question regarding ViewModel injection using dagger. So as per my understanding we have generic factory class that Implements ViewProvder.factory interface and is responsible for creating ViewModel instance when demanded from Activity/Fragment. And whatever dependencies required by that ViewModel is fulfilled by dagger itself. But what if there is a dependency say Bundle that I would like to pass to my ViewModel during it's construction. So is that possible by doing some tweaks within custom ViewModelFactory.
I know same can be with calling a simple function inside ViewModel after it's construction. But I'm Keen to know if it can be done with dagger. Any help is appreciated.
g
Yes, it's possible, but ViewModel factory should be scoped to this activity, and you just add bundle to this scope graph (activity or fragment)
But I wouldn't recommend to inject Bundle itself, better would be add bundle deserialized from bundle to graph
Ofttopic: if you asking a question in a public channel it doesn't look polite to mention someone directly imo
b
@gildor thanks for the response. Well bundle was just mentioned as an example.
With ViewModel factory scoped to particular activity/Fragment we are narrowing it down but sacrificing generic ViewModelFactory as a cost
Sure @gildor noted.
g
You still will have generic VMF, only one implementation is needed, but you don't have it in global scope, instead every screen adds it to own scope together with own view models
b
Ok so in that case how would VMF would look like. If you could share a snippet it'll be easier to co relate