Karl Einstein
Karl Einstein

Reputation: 75

Why Hilt and Dagger need to inject Activity and Fragment?

I have been using Dagger for a while and now I'm starting to migrate to Hilt, but I don't understand the benefits of injecting Activity and Fragment. In Dagger, it is AndroidInjection.inject(this) and something like HasActivityInjector

Sorry if it was a stupid question.

Upvotes: 2

Views: 411

Answers (1)

m0skit0
m0skit0

Reputation: 25873

It is not a benefit, it is a constraint imposed by Android. Activities and Fragments are instantiated by Android, not by the developer or the DI framework, and as such, they require special treatment. It is the same in Dagger actually.

In Hilt you don't need to write that code manually anymore, you can just annotate the Application, Activity and Fragment using special notations, the library will take care of generating the corresponding code.

Upvotes: 3

Related Questions