'Dagger - Hilt : Do we need to mark all activities with @AndroidEntryPoint
So my question is as I am getting started with Hilt, do we need to mark all activities with @AndroidEntryPoint annotation or can we just create a BaseActivity and extend it to AppCompactActivity and mark that single class as the entry point?
Will this work? and what, if any, are the drawback of this style.
Thanks.
Solution 1:[1]
I'm not sure if this is a comprehensive answer, maybe it's more of a personal opinion, but I'd ask what's the goal?
Are you trying to reduce boiler plate/amount of code you need to write? Then I'm afraid it'll be the same if not more, since you'll have to go to every activity and add the inheritance part.
The downside to me is that you're now using inheritance to implement something that was avoiding it. Annotations give you the opportunity to annotate any activity without saying it's a base activity. This is often better than inheritance since not every activity is a base activity and you decouple your code more from what dagger is actually doing. I think this is more of a delegate pattern or maybe more like a decorator.
That said, to answer your question, I'm not 100% this works, but to me it has the downsides of using inheritance for something that should not be modelled by inheritance.
Solution 2:[2]
You cannot mark a "base class" as an AndroidEntryPoint if it is abstract, so this idea would not work anyway. As there is usually one Activity per App (recommended by google), you should not make your life harder than it is. Just annotate an Activity with @AndroidEntryPoint and you are done here.
Solution 3:[3]
as the document says:
Classes that Hilt injects can have other base classes that also use injection. Those classes don't need the @AndroidEntryPoint annotation if they're abstract.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fred |
Solution 2 | Andrew |
Solution 3 | imansdn |