Closer look at scopes in Dagger 2
This is the last part of a write up about Dagger 2 on Android.
- Part1: Dependency injection on Android
- Part2: Dagger 2 usage on Android
- Part3: Closer look at scopes in Dagger 2 (this)
All usages of Dagger below refer to Dagger 2
Scopes
In Dagger 2 we can use the @Singleton
scope from JSR 330 or we can define our own scopes. Custom scopes are defined as annotations like this.
@Scope
@Documented
@Retention(value=RetentionPolicy.RUNTIME)
public @interface CustomScope {
}
We use custom scope when we want to reuse dependencies for a custom amount of time. For example, we could