![]() |
In order to fully understand the concept of Component in Dagger, we have to first go through a brief about Dagger itself. Now according to the documentation Dagger is defined as follows :
Let us analyze what the above statement means. Imagine the following scenario: Let us assume we have a class, Bike Class, and two functions, it requires two more classes, namely, Engine and Wheels. Thus we say that Bike Class is dependent on Engine and Wheels and for proper functionality, these dependencies should be implemented. Now in order to instantiate the Bike Class, we also have to create and initialize or precisely, inject the other two classes in the activity’s boilerplate code only. This is depicted as follows : ![]() Instantiating Bike Class without Dagger Now when the project scales in size there are generally a lot of dependencies among various classes and this above approach can lead to some serious issues and performance bottleneck. This is where Dagger comes into play and it automatically takes care of all the dependencies and instantiation. To have a detailed insight about dagger and dependency injection: Dependency Injection with Dagger 2 in Android Component in DaggerNow Component in a Dagger works by creating a graph of all the dependencies in the project so that it can find out where it should get those dependencies when they are needed. In order to implement this, an interface needs to be created and should be annotated with @Component. With this annotation processor, Dagger creates a dependency graph that consists of the relationships between various classes. This allows Dagger to create a container as we would have done with manual dependency injection. Functions can also be defined inside the @Component interface that returns the instances of the classes needed (Bike Class). @Component informs Dagger to generate a container with all the dependencies required. This is known as Component in Dagger. Let us see the above example in implementation. Step by Step ImplementationStep 1: Create a New Project To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Step 2: Adding Required Dependencies In order to use dependency injection with the help of dagger, we need to add some dependencies to the Gradle file. Go to Gradle Scripts –> build.gradle(Module: app) and add the following dependencies. After adding both of these dependencies you need to click on Sync Now.
Now let us customize some color attributes to enhance the app bar. Go to app > res > values > themes.xml and customize the following color attributes. XML
Step 3: Setting up the activity_main.xml file In this step, we will be creating the layout of the app. A text box and a button needs to be created where the text view will show some default text and after clicking the button a bike entity will be created and the user will be updated in the text box. Below is the code snippet. XML
Step 4: Working with the BikeComponent Interface In this step, we will create an interface with the annotation @Component which will be responsible for instantiating the bike entity and thus creating the graph of all the dependencies. You may refer to this article to create Interface in Android studio. Java
Step 5: Creating all the necessary classes required for the bike(Bike, Engine, and Wheels) In this step, we will inject the required classes with annotation @Inject to create the Bike entity. Below is the code snippet for all the files. You may refer to this article to create Class in Android studio. GFG_Bike.java Class: Java
GFG_Engine.java Class: Java
GFG_Wheels.java class: Java
Step 6: Working with the MainActivity.java file In this step, we will instantiate the GFG_Bike. If the step is successful the text view will be updated else an Error Toast will be shown. Below is the code snippet. Java
Output: Output: On Logcat ![]() Snapshot of Logcat |
Reffered: https://www.geeksforgeeks.org
Android |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |