Closer look at scopes in Dagger 2

This is the last part of a write up about Dagger 2 on Android.

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

...

Dagger 2 usage on Android

This is the second part of a write up about Dagger 2 on Android.

All usages of Dagger below refer to Dagger 2

Component lifecycle / Singleton

Dagger, and other DI-frameworks, have the concept of scopes to decide if an instance should be re-used or not. If we don't specify any scope, like in part 1, we'll get a new instance every time we ask for a dependency. That can be suitable for some cases but often we want to reuse

...

Dependency injection on Android

This is the first part of a write up about Dagger 2 on Android.

Dagger 2

Dagger 2 is a dependency injection framework for Java built on the JSR 330 standard. It works in all Java projects but this article will focus on Android development where Dagger is de facto standard for dependency injection.

The latest version, Dagger 2, is developed by Google together with Square, the original developers of Dagger.

Dagger 2 works at compile time in comparison to

...