问题
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