Dagger - Kotlin - @Singleton - component dependencies - 10

Опубликовано: 07 Май 2026
на канале: Mango Burrito
205
5

In part 10 of this Dagger tutorial, I will show you how to create and use custom scopes and add dependencies to a Component.
This is useful if you want to reuse single instances of dependencies for example in an activity or fragment, but not keep them as application-wide singletons that stay in memory for the whole lifetime of the app.
To achieve this, we have to create a custom Annotation and use it with a Dagger Component that lives as long as that scope.
For instance, suppose you have a use case where you want something to stay in memory during the lifecycle of an activity or as long as a user is authenticated. If we want to create an application-wide singleton, we have to instantiate its component in the Application class and access it from everywhere else through a call to getApplication.
To connect 2 components of different scopes into a single dependency graph, you can make the outer component a dependency of the inner component, then pass it as a Builder argument. The outer component has to expose dependencies to the inner component explicitly via provision methods (@Binds or @Provides or @Inject).

GitHub: https://github.com/ktenzweiler/LearnD...

FB: https://www.facebook.com/profile.php?...
IG:   / texdroid01