Dagger2 Cheat Sheet #2

Murat Karaöz
4 min readSep 11, 2018
  1. What is a scope?
  2. What should I keep in mind when implementing scopes?
  3. What if I don’t use a scope?
  4. What is @Singleton scope?
  5. How do I create my own scope?
  6. I want to learn everything about scopes!
  7. What is Dagger-Android module?
  8. What is DaggerAppCompatActivity and DaggerFragment?
  9. How do I inject ViewModels?
  10. When do I use @Binds over @Provides?

Check out Part 1 for more.

What is a scope?

A scope cares about keeping single instance of class as long as its scope exists. In practice, it means that instances scoped in @ApplicationScope lives as long as Application object. @ActivityScope keeps references as long as Activity exists (for example we can share single instance of any class between all fragments hosted in this Activity).

Just to be clear — there are no @ActivityScope or/and @ApplicationScope annotations provided by default in Dagger 2. It’s just most common usage of custom scopes. Only @Singleton scope is available by default (provided by Java itself). (See: Dagger 2 — Custom scopes)

What should I keep in mind when implementing scopes?

  1. Usually the scope annotations are set for the Component and provide method.
  2. Scope is not necessary for parameters stored within the module. Because the scoped provider isn’t needed for it to provide the same instance. It will only exist once as it is always provided from the field in the module.
  3. If at least one provide method has a scope annotation the Component should have the same scope annotation.
  4. Any time a non-scoped service is being injected by the same component, a new instance of a service is created.
  5. The Component can be unscoped only if all provide methods in all its modules are unscoped too.
  6. All scope annotations for one component (for all modules with provide methods to be a part of Component and for the Component itself) should be the same.
  7. First time a @Singleton scoped service is being injected, it is instantiated and cached inside the injecting component, and then the same exact instance will be used upon injection into other fields of the same type by the same component.
  8. Custom user-defined scopes are functionally equivalent to a predefined @Singleton scope.
  9. Injection of scoped services is thread safe.
  10. Two dependent components cannot have the same Scope. More details here.

What if I don’t use a scope?

Everytime you inject a dependency a new instance is created.

What is @Singleton scope?

When you use singleton scope, first injected dependency will be cached and all fields would end up having a reference to the same object as long as the component is alive. If the component is destroyed and re-created all cache is lost and everything will be created again.

How do I create my own scope?

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomScope {}

What is DaggerAppCompatActivity and DaggerFragment?

If you are in to inheritance you can check these out. Author, Mert Şimşek, advises to use these over interfaces, I don’t. But at the end of the day, it is matter of taste.

How do I inject ViewModels?

I could not figure it out until I read this topic at Reddit:

I used to create a factory for each viewmodel but then found this:

A complete example is here:

When do I use @Binds over @Provides?

@Binds provides a replacement of @Provides methods which simply return the injected parameter. If you have provide methods which just call constructor of implementation classes to inject interfaces, you can use @Binds annotation instead to get rid of boilerplate code in your dagger module.

--

--