Note: Examples on this page assume usage of the Gradle plugin. If you are not using the plugin, please read this page for details.
Android types
Once you have enabled members injection in your Application
, you can start
enabling members injection in your other Android classes using the
@AndroidEntryPoint
annotation. You can use @AndroidEntryPoint
on the following types:
- Activity
- Fragment
- View
- Service
- BroadcastReceiver1
Note that ViewModels are supported via a separate API
@HiltViewModel
. ContentProviders are not directly supported
due to their onCreate
being called at startup, but you can access dependencies
via an entry point.
The following example shows how to add the annotation to an activity, but the
process is the same for other types. When adding to other types, note that as a
general rule, Hilt types need to be attached to other Hilt types to work. So
before adding [@AndroidEntryPoint
] to a fragment, the activity must be
annotated as well.
To enable members injection in your activity, annotate your class with
@AndroidEntryPoint
.
@AndroidEntryPoint
public final class MyActivity extends MyBaseActivity {
// Bindings in SingletonComponent or ActivityComponent
@Inject Bar bar;
@Override
public void onCreate(Bundle savedInstanceState) {
// Injection happens in super.onCreate().
super.onCreate();
// Do something with bar ...
}
}
@AndroidEntryPoint
class MyActivity : MyBaseActivity() {
// Bindings in SingletonComponent or ActivityComponent
@Inject lateinit var bar: Bar
override fun onCreate(savedInstanceState: Bundle?) {
// Injection happens in super.onCreate().
super.onCreate()
// Do something with bar ...
}
}
Note: Hilt currently only supports activities that extend ComponentActivity
and
fragments that extend androidx library
Fragment
,
not the (now deprecated)
Fragment
in
the Android platform.
Retained Fragments
Calling setRetainInstance(true)
in a Fragment’s onCreate
method will keep a
fragment instance across configuration changes (instead of destroying and
recreating it).
A Hilt fragment should never be retained because it holds a reference to the component (responsible for injection), and that component holds references to the previous Activity instance. In addition, scoped bindings and providers that are injected into the fragment can also cause memory leaks if a Hilt fragment is retained. To prevent Hilt fragments from being retained, a runtime exception will be thrown on configuration change if a retained Hilt fragment is detected.
A non-Hilt fragment can be retained, even if attached to a Hilt activity. However, if that fragment contains a Hilt child fragment, a runtime exception will be thrown when a configuration change occurs.
Note: While it’s not recommended, Hilt fragments can be detached and
reattached to the same activity instance. In this case, the Hilt fragment will
only be injected on the first call to onAttach
. Note that this is not the same
as retaining a fragment, because a retained fragment will be reattached to a
different instance of the activity. Again, this is not recommended, and it is
often less confusing to just create a new fragment instance for each usage.
Views with Fragment bindings
By default, only SingletonComponent
and ActivityComponent
bindings can be
injected into the view. To enable fragment bindings in your view, add the
@WithFragmentBindings
annotation to your class.
@WithFragmentBindings
@AndroidEntryPoint
public final class MyView extends MyBaseView {
// Bindings in SingletonComponent, ActivityComponent,
// FragmentComponent, and ViewComponent
@Inject Bar bar;
public MyView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
// Do something with bar...
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
// Find & assign child views from the inflated hierarchy.
}
}
@AndroidEntryPoint
@WithFragmentBindings
class MyView : MyBaseView {
// Bindings in SingletonComponent, ActivityComponent,
// FragmentComponent, and ViewComponent
@Inject lateinit var bar: Bar
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
init {
// Do something with bar ...
}
override fun onFinishInflate() {
super.onFinishInflate();
// Find & assign child views from the inflated hierarchy.
}
}
-
Unlike the other supported Android classes,
BroadcastReceivers
do not have their own Dagger component and are instead simply injected from theSingletonComponent
. ↩