Hello everyone. Welcome back. When we are using dagger for a class
if we cannot access the constructor, if we cannot annotate the constructor with inject annotation
we will not be able to get dependencies directly.
Here in this example project, To get the SmartPhone dependency, we have invoked this getSmartPhone method
form the MainActivity, we called to that method using the , smartPhoneComponent interface.
Because we cannot annotate the constructor of the MainActivity.We don’t have a constructor here.
But this is not the best and most efficient way to do it.
There can be many activities and fragments in a project. And there can be many dependencies.
Think, If you have 10 required dependencies like this SmartPhone dependency, you may have to write getter
methods for all of them in the component interface. And you have to call to them form all activities
and fragments in this way.
And this becomes more difficult if you have to pass values to those dependencies at runtime.
Dagger library doesn’t expect us to write getter methods for dependencies like this.
We can just get them injected to the activities or fragments easily using field injection.
Let me show you how to do this,
Go to SmartPhoneComponent. Comment this getter method. We don’t have to write getter methods like this.
using an indicator method we can inject all dependencies belong to the dependency graph. Now, I am creating an abstract
inject method for the MainActivity. void inject
Parameter should be MainActivity.
Here I used inject as the method name. But you can use any method name. But as a convention we use inject as
the name of the injector method. We are going use this method for the main activity.
So I added an instance of main activity as a parameter. If in case you need to use this component in
3 activities and one fragment, you may need to write four inject methods here with those activities and fragments
as parameters. Now let’s go back to MainActivity. We can comment this getSmartPhone method invocation part.
We can invoke the inject method here, passing this as a parameter. But what is the difference. IF we
invoke a method like getSmartPhone. We will be able to only get the SmartPhone dependency.
Now we can get any dependency to the MainActivity using field injection.
Let me show you.
We only need to add inject annotation to this SmartPhone object reference variable.
If you run this project it will not give any null pointer exception
instead it will run as it did before, because we have provided smartphone dependency via field injection.
Let’s run the app and see how it works.
Yes, app is working as it worked before. This approach is very easy. Let’s say you want a battery
instance in the main activity. You just need to declare a battery reference variable and annotate it with
inject annotation
If you want a SIMCard instance you can just declare a SIMCard reference variable and annotate it with
inject annotation.
So that's how we can do it.
So that the the end of this lesson.
During the next lesson I will show you how to provide values to dependencies dynamically. How to provide values to dependencies at run time.
Thank you very much for listening.
See you soon
with the next lesson.