Part 4: Dagger For Noob Android Developer

PAUL MATHEW
6 min readDec 8, 2022

--

@Singleton and @Scope

In this part we are going to look into Singleton in Dagger. I hope everybody know what is singleton and where we can use that. So not going deeply into that part. But still some small detail about it.

We use singleton in android when we want same object everywhere with same reference, then we are using it. Like if we are calling network call with Retrofit or database call with RoomDB we don’t want multiple object of each property or class wherever we call them or access them. In order to prevent multiple creation of same object we use singleton.

In dagger we make a function or a module singleton using @Singleton

In this image if you observe line number8 I have added one annotation that is @Singleton . What this will do is, it will only create one instance of UserInteractionComponent class object. Like in the bellow image we can use singleton with Dagger.

In line number 11 I have used @Singleton and that makes the class EmailService singleton and whenever we call the EmailService Class only one object will be created not many.

But there is catch for this, Singleton can be in multiple ways.

  1. Application Level : In this type one singleton object created will keep the value till the end of the application/
  2. Activity Level : in this type the life time is till the end of an Activity.
  3. Fragment Level : in this type life time is till the end of the fragment.

So when we create a Singleton using dagger we have to be precise when we need to create and where we needs to create.

When we create or build a component it will create a new instance of the Singleton object also.

The reason why I just made the above statement is. For example if we rotate the screen that means activity is created again and all the things inside the onCreate() will be call again and new object will be created for each of them.

Like new initialization if we create duplicate object of same component then also new object of the same singleton item can be created.

The reason for the above statement is just adding @singleton in a component class or over a function or over a module won’t make the object singleton throughout the application lifetime. All the singleton under this component is singleton only for that particular Component.

To make a component object same throughout the application we need to create or build a component inside an Application class.

Lets create an Application class and define in the Manifest file also.

package com.tutorials.daggerformedium

import android.app.Application

class MyApplication: Application() {

lateinit var userInteractionComponent: UserInteractionComponent
override fun onCreate() {
super.onCreate()
userInteractionComponent=DaggerUserInteractionComponent.factory().create("That title string")
}

}

Then use the component object like this wherever you want.

Since we have created the component inside the Application class then all singleton inside the component are in Application level.

Singleton is a scope objects and Scope is inside the component. And Scope is defined using @Singleton annotation.

We can create our own scope also. In order to do that just create an annotation file in kotlin like bellow.

import java.lang.annotation.Documented
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import javax.inject.Scope


@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME)
annotation class NewScope()

and we can use this scope class instead of @Singleton like bellow

Custom Scopes and Component Dependencies for Dagger2

In this part let’s look into how we can connect two Component class in Dagger.

For this I have created on new interface class like below for logging or adding analytics service to the application.

Then let’s create a module class for this interface class. Like how did in the first parts.

then let’s create a component class like we created previously (Almost all of the procedures are same here).

In the component class we mentioned that it is a singleton class by using @Singleton and there is a function in the same class for getting the AnalyticsService we want.

Then we will make a change in the application class like below.

Previously this class was used to create userInteractionComponent object and thus making all the functions and modules associated with it application level singleton. By removing that here will change the singleton property into either Activity level singleton or fragment level.

Here we are createing object of Appcomponent class and thus making the AnalyticsService objects and fucntions Application level singleton.

Now we have done the already known part of creating component and making object of the component.

Now lets connect Appcomponet with UserInteractionComponent.

In this image you can see that I have added AnalyticsService object into RoomDBRepo class as well as ServerDBRepo class and use the property inside the Analytics Service over here.

But how are we passing the object of AnalyticsService class into these two classes. Dagger is doing that for us. Again this AnalyticscService is not a part of UserInteractionComponent then how do Dagger create and pass the object of AnalyticsService ?

There comes the Component Dependency. We need to tell the UserInteractionComponent that the modules related to you are dependent of AnalyticsService. And you need to do something for that. Inorder to do that we change some codes in UserInteractionComponent like below.

Here we add dependencies = [AppComponent::class] into the @Component annotation before modules=[]. What this tells to UserInteractionComponent that this component is dependent to that components mentioned inside the dependencies=[]. And if you again observe the image at line number 17 we added a new variable into the create () that is appComponent:Appcomponent . This means we need to initialize the AppComponent somehwere else and pass it into the create() when we initialize UserInteractionComponent.

In this example we are creating the AppComponent inside the Application class and thus making is global throughout the application.

This is the new MainActivity. In this code we can see that appComponent is taken from the MyApplication class and it is passed into the create() in line number 22 during the creation of userInteractionComponent.

Previous Parts for this series.

Part 1 of this Dagger2 For noobs

Part 2 of this Dagger2 For noobs

Part 3 of this Dagger2 For noobs

--

--

PAUL MATHEW

A Noob android developer who try to make some notes which obtained by going through different tutorials and stackoverflow.