Dagger 2 Deep Dive (24/55) - Matching Dagger Component Scopes with ViewModel Scopes

Опубликовано: 30 Сентябрь 2024
на канале: Brandon Gogetap
149
3

In this lesson we learn how to make your Fragment’s component lifecycle match the Jetpack ViewModel lifecycle. This opens up possibilities (including not needing to rely on using ViewModel directly) for retaining state during your Fragment’s “user perceived” lifecycle.

I say “user perceived” because, your user doesn’t care that your Fragment was torn down due to a configuration change. As far as they’re concerned, they are looking at the same screen. I’ve found that the dependencies needed for each screen generally mirror that lifecycle as well.

This strategy is completely optional—use what works best for your use case—however I encourage it if you’re using injected ViewModels. If your dependencies are recreated on config change because you create a new Component, but you’ve already created the ViewModel for that screen, you may end up with mis-matched shared dependencies in injected components outside of your ViewModel.

Project: https://github.com/bgogetap/GitHubBro...

This video is part of a series. See the full playlist here:    • Dagger 2 Deep Dive