I have implemented dagger2 v2.2 previously but as now they have added dagger.android part also. so I am creating sample project with that.
I am aware about old methodology of @Provide and @Modules and @Components etc annotations but from Dagger 2.8+ they have added this android-support library also which have some new Injections like @ActivityKey, @ContributesAndroidInjector, @Subcomponent.Builder etc.
So my question is what benefits it brings to the table.
Does it resolve problems like Inject method with base class can work for all child class ? or Any other benefits ?
2nd question - HasFragmentInjector is just to load fragment inside activity like we used to do using fragment manager ? or I am missing some thing ?
Please don't downvote its more informative question for all library users as documentation of library don't provide such answers.
First question
This has already been answered in What are the advantages of using DispatchingAndroidInjector and the other dagger-android classes?
Dagger 2 uses code generation at compile time for dependency injection. In this, it is different from other dependency injection frameworks like Guice that inspect injection sites at runtime. In order for Dagger 2 to work, you must at some point specify the invariant of the injection site. Therefore it will never be possible to write something like:
inside a Dagger 2 component and have it inject all activities.
However, there are many improvements with the new classes available in dagger-android. Whereas before you would have to write:
and so on for each different injection site you can now write the following code:
and then:
inside your MainActivity at the appropriate point.
Second question
HasFragmentInjector
simply marks the class where the Fragment should get itsAndroidInjector
from. You can see for yourself in the code on GitHub forAndroidInjection#inject(Fragment fragment)
:From the javadoc, this method walks first the parent-fragment, then the Activity, then finally the Application to find
HasFragmentInjector
and uses theAndroidInjector<Fragment>
to inject the fields of the Fragment.However, the presence of
HasFragmentInjector
does not mean that you should start managing Fragments using Dagger 2:You should still use the idiomatic way of instantiating Fragments which is using static factory methods. Dagger 2 will perform injection for the fields inside the Fragments when their
onAttach(Context context)
is invoked when, say, you add the Fragment using a transaction or you delegate to a ViewPager. Instead of the above example, the following code is a very simple Activity with a ViewPager and two Fragments:The FragmentStatePagerAdapter correctly handles the management of the Fragments and we do not inject as fields inside MainActivity.
The Fragments themselves look like this:
in CoffeeFragment.java:
in CoffeeFragmentModule.java:
in CoffeeFragmentSubcomponent.java:
in CoffeeRepository.java:
Official Documentation explains this topic quite well in my oppinion.
Anyway the main benefit is that instead of something like this
You can simply write this, which makes life easier for everyone.
Less boilerplate, easier maintainability.
The previous approach is kind of breaking the basic concept of dependency injection, a class should not know any details about the way dependencies are being injected.