是否可以使用dagger2注入内容视图 [英] is it ok to inject content view using dagger2

查看:70
本文介绍了是否可以使用dagger2注入内容视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的android项目中使用了数据绑定,而且我还在 DI 中使用了 dagger 2 .

I'm using data binding in my android project, also i'm using dagger 2 for DI.

主要用于设置具有数据绑定的内容视图,我需要执行以下操作:

basically for setting content view with data binding i need to do something like this :

LayoutClass layoutClass = DataBindingUtil.setContentView(Activity, Layout);

我在匕首模块中提供了 layoutClass 并将其注入到我的活动中.问题是,这是一个好习惯吗?

I'm providing that layoutClass in dagger module and injecting it to my activity. the question is, is this a good practice ?

推荐答案

从技术上讲,您正在以此定义圆参考.只是没有警告您,因为设置图表需要您对此有所准备.

Technically you're defining a circle-reference with this. You're just not warned, because setting up the graph requires you to be pro-active about this.

依赖关系看起来像 activity->布局->活动,同时为模块明确提供活动.另外,您正在使用 DataBindingUtil.setContentView()修改活动,因此提供了对活动的依赖,这实际上是活动本身的属性.

The dependencies would look like activity -> layout -> activity while you provide the module with an activity explicitly. Additionally you're modifying the activity with DataBindingUtil.setContentView() and therefore provide a dependency to the activity, which actually is a property of the activity itself.

因此,切勿为Dagger提供任何UI.尤其是不参加活动.

So, never provide any UI with Dagger. Especially not to an activity.

这篇关于是否可以使用dagger2注入内容视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆