Search code examples
androidkotlindaggerdagger-hilt

Dagger-Hilt : Why we must annotate Activities which has no injection


Why we must to annotate activities which contains fragments on it ?

Activity has no @Inject but fragments need some dependencies . I annotate fragments as @AndroidEntryPoint but crash until set this on parent activity.


Solution

  • You can't start a Fragment without an Activity. You have to annotate your Activity with @AndroidEntryPoint because your fragment is a HiltComponent now. When you don't annotate your Activity with @AndroidEntryPoint Hilt would not create a component for this activity and thus could not start the fragment because it didn't create the ActivtyComponent yet.

    Hilt activities need to be attached to Hilt applications. Hilt fragments must be attached to Hilt activities.

    See here: https://dagger.dev/hilt/migration-guide (2. Migrate Activities and Fragments)