I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass?
Take Activity for example. I have a BaseActivity
that that every Activity extends from. Is there a way that I can just create an inject method in the component for BaseActivity and just call inject in BaseActivity's onCreate, and @inject fields in sub activities get injected automatically?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
It cannot be done right now. Explanation by Gregory Kick (from here):
This issue was discussed here and here, follow up these for updates. But it is unlikely to change soon, cause Dagger 2 is close to release.
I encountered the same situation. One way to ease a bit the injection from a common component in all Activities is the following:
1) Extend the Application class to be able to create the common component and keep a reference to it.
2) Create an abstract DaggerActivity which gets the common component from Application and calls an abstract method
injectActivity
, giving the component as an argument. Like this:3) Last, you have to actually inject each
Activity
extendingDaggerActivity
. But this can be done with less efforts now, as you have to implement theabstract
method otherwise you'll get compile errors. Here we go:Of course, you still have to declare each Activity explicitly in your Component.
UPDATE : Injecting @ActivityScope objects into Fragments
At some point, I needed to use custom scopes to bind objects to an
Activity
life cycle. I decided to extends this post as it might help some people.Let's say you have a @Module class
ActivityModule
and a @Subcomponent interfaceActivityComponent
.You would need to modify the
DaggerActivity
. TheActivities
extendingDaggerActivity
would need to implement the new method (change of signature).Then, a class
FragmentDagger
extendingFragment
can be created like this :As for the
Activities
, theFragments
extendingFragmentDagger
have only one method to implement:You should be able to reuse
Fragments
wherever you want. Notice that the methodsuper.onCreated()
inActivityDagger
should be called after the component instantiation. Otherwise, you will get NullPointerException when theActivity
state is recreated, because the methodsuper.onCreate()
of theFragment
will be called.You can do a little hack using reflection:
In this case, you still need to write inject method in a component, but you don't need 'inject' method in each activity, fragment, view, whatever.
Why it's work? when we use
getClass()
on injection subject will get a descendant class, not base.Caution! In case you use Proguard, you need to add next
-keep class <ComponentClass> { *; }
to your rules in order to keep inject methods as is in component