Recent Posts

Those who are free of resentful thoughts surely find peace. - Buddha

Dagger2 - Part4

Posted on 29th Oct 2017


<-Back to Blogs

In  Dagger2 - Part3 we learnt what Components & Modules are, now its time to learn how we can generate the DAG.

Step 11:

We have created MainActivity which conisted of ActivityModule which was used in the the DaggerActivityComponent to build the DAG link here.

@Module
public class ActivityModule {

private Activity mActivity;

public ActivityModule(Activity activity) {
mActivity = activity;
}

@Provides
@ActivityContext
Context provideContext() {
return mActivity;
}

@Provides
Activity provideActivity() {
return mActivity;
}
}

@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {void inject(MainActivity mainActivity);}

Note: ActivityComponent specify ApplicationComponent and ActivityModule . ApplicationComponent is added to use the graph that has already been generated in the previous step and do exists because the DemoApplication class persists till the application is running.

@PerActivity is a scope and is used to tell the Dagger that the Context andActivity provided by the ActivityModule will be instantiated each time anActivity is created. So, these objects persist till that activity lives and each activity has its own set of these.

We may ask that the DataManager will then be created with each Activity. But that is not true because we have annotated the DataManager class with @Singleton. Which brings the class in the global scope and thus is accessed whenever a dependency is expressed.

Now let’s revisit the DemoApplication class and MainActivity class. These classes don’t have a constructor and Android System is responsible for instantiating these. To get the dependency we use the OnCreate method as it is called once when they are instantiated.

applicationComponent = DaggerApplicationComponent
.builder()
.applicationModule(new ApplicationModule(this))
.build();
applicationComponent.inject(this);

DaggerApplicationComponent is the generated class by the Dagger, implementing the ApplicationComponent. We provide the ApplicationModuleclass that is used to construct the dependencies.

We have also called the inject method of applicationComponent and passed the instance of the DemoApplication class. This is done to use it for providing the DataManager.ApplicationComponent instance is retained so as to access all the classes that are available in the dependency graph and is express for access.

public ActivityComponent getActivityComponent() {
if (activityComponent == null) {
activityComponent = DaggerActivityComponent.builder()
.activityModule(new ActivityModule(this))
.applicationComponent(DemoApplication.get(this).getComponent())
.build();
}
return activityComponent;
}

Similar process is applied in the MainActivity as well. The only difference is that, we also provide ApplicationComponent that is required for dependencies resolution mentioned in ActivityModule.

This completes the Example project.

Let's summarize what we learnt in this Dagger2 series below with the link:

Part1 - What is Dagger2? Why do we need Dependency Injection?

Part2 - Implementations -

  1. Part2 - We focussed on implementing the DI using Dagger2. We designed the classes neccesary which later we could use in the DemoApplication of Android.
  2. Part3 - In this part we started using the DI which we designed in the Part2. We
  3. Part4 - In these parts we learnt how DaggerApplicationComponent gets generated when we provide the ApplicationModule class that is used to construct the dependencies.

 The dagger2 articles ends here, hope some of you have find it informative. Please do let me know your view by commenting below. Happy learning!


<-Back to Blogs

Categories

Good, better, best. Never let it rest. Untill your good is better and your better is best. - St. Jerome

© SOFTHINKERS 2013-18 All Rights Reserved. Privacy policy