Skip to main content

Android in Plain English - Dagger

A lot of development articles I find tend to either explain things at too high of a level or too low of a level, so I either don't know how to get started with their idea or I'm too overwhelmed with all the possibilities of what I can do. That's why I want to start a series called Android in Plain English where I explain (in simple terms) some various Android topics and give an introduction on how to use them.

For my first explanation, I'll start with Dagger. If you've spent much time learning about Android, you've likely heard about Dagger. You've also likely heard how it can be very complicated but also very powerful. In brief, Dagger is a dependency injection framework that helps you manage instantiating classes and other dependencies in your app. Dagger uses annotations to build a "dependency graph" and inject dependencies where they're needed.

Basic classes (i.e. things that can be instantiated through their constructor) can have their constructor annotated with `@Inject` and Dagger can figure out how to inject them automatically. Dagger needs to be able to create every dependency in the constructor - either through its own constructor or through a Module/`@Provides`.

Modules are a way to tell Dagger how to create dependencies that are more complicated than using a constructor. For example, to create an instance of Retrofit you have to use a Builder. Dagger can't figure out how to use a builder, so you need to create a Module class with a function that "provides" the Retrofit implementation. You tell Dagger that the function provides an implementation by annotating it with `@Provides`. Now whenever Dagger tries to inject something like complicated like Retrofit, it goes to the Module and runs that function to get the implementation for injecting. There are other annotations you can add to the function that change how Dagger uses it. I think the most common is `@Singleton` which tells Dagger that the dependency should be treated like a singleton (duh).

One small detail is that `@Singleton` is what is known as a "Scope". Scopes define how long the lifecycle for a module or dependency should be compared to the component. A Singleton is app-wide. You can define custom Scopes that limit the "lifecycle" to be a subset of the app, like only within certain screens. By default, a Component is "unscoped" and an unscoped component cannot contain scoped modules. Therefore if you have a Singleton defined in a module, any component that includes it must also be annotated as a Singleton.

Some links I've found helpful when learning Dagger:
https://gaurav.chaddha.me/dagger2
https://proandroiddev.com/dagger-2-part-ii-custom-scopes-component-dependencies-subcomponents-697c1fa1cfc

Comments

  1. Sands Casino in Las Vegas, Nevada | SRI, NV
    › gaming › sg › gaming › sg Sands Casino is located in the heart of Las Vegas, Nevada, about a 샌즈카지노 1-minute drive from Sands Arena. Sands Arena is the casino's newest destination

    ReplyDelete

Post a Comment

Popular posts from this blog

2020 Goals

The new year is a good time to plan out goals for the upcoming 12 months. It's a long enough time frame that almost anything seems achievable, but also short enough that it doesn't feel like you have to wait forever to see the result. One of my problems with long-term planning is everything feels so far in the future and I want to see results NOW. Saving for retirement is great (and I do), but it's very annoying to want to do something next month and my money is tied up for the next 40 years. So anyway, here is some of what I want to get done in 2020: Read 12 books My first thought was to read a book a month, but due to the nature of the fantasy genre I tend to choose large tomes. So I chose a fixed number to allow for larger and smaller books. I like to alternate between several styles of books: sci-fi, fantasy, and nonfiction. I have a coworker that reads a lot of science fiction so I get a lot of recommendations from him. Sometimes I'll hear about an interest...