From the course: Android Dependency Injection with Dagger 2 and Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Real-world example: Architecture components #2

Real-world example: Architecture components #2

From the course: Android Dependency Injection with Dagger 2 and Kotlin

Start my 1-month free trial

Real-world example: Architecture components #2

- [Instructor] In a real world example, we've enabled field injections so that Dagger can give our activity a View Model factory. What we haven't done is told Dagger how to create that View Model factory. So let's do that. What we have to do here is teach Dagger to create the field that's being injected into the activity, the View Model factory. Well that part's easy. Let's just annotate the constructor with an @ sign inject annotation. The only thing that's left to do is to explain to Dagger how to create the map on which the View Model factory depends. This is the most complicated part. We need to create a map whose keys are classes and whose values are providers of View Models. That's View Model factories. First of all, remember, the Dagger matches types exactly. Our implementation of View Model, View Model one, is not exactly the type of View Model. The View Model is defined over in the activity file, let's look at it. Our View Model is type View Model one and it extends View…

Contents