is it ok to inject content view using dagger2

拥有回忆 提交于 2020-12-13 04:10:37

问题


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);

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


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/44549355/is-it-ok-to-inject-content-view-using-dagger2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!