Devrath
Devrath

Reputation: 42824

Can Dagger Perform Injection of view Objects in Android

I am Exploring Dagger for the dependency injection in Android .

What I discovered:


Questions:

What I am trying to find:

  1. Can we inject view objects like ImageView, Button etc?
  2. If possible how to perform it?
  3. If not possible what is the best alternative (I am aware Roboguice is deprecated)?

Upvotes: 1

Views: 310

Answers (1)

David Rawson
David Rawson

Reputation: 21417

Can we inject view objects like ImageView, Button etc?

Since you mentioned Roboguice as a point of reference in your question, I am taking this question to mean can you do something the following in Dagger 2:

@InjectView(R.id.my_view) View view;

The answer to this question is 'no'. In other words, Dagger 2 includes no special syntax for injecting Views that would otherwise be obtained through findViewById(int id).

If not possible what is the best alternative (I am aware Roboguice is deprecated)?

Butterknife is a good companion to Dagger 2 in that it uses the same approach of code generation to allow you to inject views.

Alternatively, the Data Binding Library will allow you to do this.

Upvotes: 1

Related Questions