简体   繁体   English

Dagger - Hilt:我们是否需要用@AndroidEntryPoint 标记所有活动

[英]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?所以我的问题是,当我开始使用 Hilt 时,我们是否需要使用 @AndroidEntryPoint 注释标记所有活动,或者我们是否可以创建一个 BaseActivity 并将其扩展到 AppCompactActivity 并将单个 class 标记为入口点?

Will this work?这行得通吗? and what, if any, are the drawback of this style.这种风格的缺点是什么,如果有的话。

Thanks.谢谢。

You cannot mark a "base class" as an AndroidEntryPoint if it is abstract, so this idea would not work anyway.如果它是抽象的,则不能将“基类”标记为 AndroidEntryPoint,因此这个想法无论如何都行不通。 As there is usually one Activity per App (recommended by google), you should not make your life harder than it is.由于每个应用程序通常有一个 Activity(由 google 推荐),因此您不应该让您的生活变得更加艰难。 Just annotate an Activity with @AndroidEntryPoint and you are done here.只需使用 @AndroidEntryPoint 注释一个 Activity 即可。

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.那么恐怕它会是相同的,如果不是更多的话,因为你必须 go 到每个活动并添加 inheritance 部分。

The downside to me is that you're now using inheritance to implement something that was avoiding it.对我来说不利的是,您现在正在使用 inheritance 来实现一些避免它的东西。 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.这通常比 inheritance 更好,因为并非每个活动都是基本活动,并且您将代码与 dagger 实际执行的操作更多地分离。 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.也就是说,要回答您的问题,我不是 100% 可行,但对我来说,使用 inheritance 来处理不应由 inheritance 建模的东西有缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM