What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android
ActivityRetainedComponent lives across configuration changes, so it is created at the first onCreate and last onDestroy, and when you mark your dependencies in ActivityRetainedComponent with @ActivityRetainedScope its guarantees that your object will be a singleton and survive across configuration changes.
In Dagger, we create scope annotations such as ActivityScope, FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.
Instead, Hilt offers predefined components that are generated for you. Hilt comes with a built-in set of components (and corresponding scope annotations) that are automatically integrated into the various lifecycles of an Android application. The diagram below shows the standard Hilt component hierarchy.
Annotation Type AndroidEntryPoint Marks an Android component class to be setup for injection with the standard Hilt Dagger Android components. Currently, this supports activities, fragments, views, services, and broadcast receivers.
For example, a binding within an @InstallIn (ActivityComponent.class) module can only be scoped with @ActivityScoped. When using Hilt APIs like @AndroidEntryPoint to inject your Android classes, the standard Hilt components are used as the injectors. The component used as the injector will determine which bindings are visible to that Android class.
If the binding that you wanted to retrieve were in the ActivityComponent, you would instead use the ActivityContext. Hilt is built on top of the Dagger dependency injection library, providing a standard way to incorporate Dagger into an Android application. To simplify Dagger-related infrastructure for Android apps.
The Hilt module AnalyticsModule is annotated with @InstallIn (ActivityComponent::class) because you want Hilt to inject that dependency into ExampleActivity. This annotation means that all of the dependencies in AnalyticsModule are available in all of the app's activities. Interfaces are not the only case where you cannot constructor-inject a type.
Both Dagger and Hilt have a concept of Component which is a dependencies container that follows the Android lifecycle. But unlike Dagger, Hilt users never define or instantiate Dagger components directly. Instead, Hilt offers predefined components that are generated for you.
Based on the documents. ActivityRetainedComponent
lives across configuration changes, so it is created at the first onCreate and last onDestroy, and when you mark your dependencies in ActivityRetainedComponent
with @ActivityRetainedScope
its guarantees that your object will be a singleton and survive across configuration changes. But ActivityComponent
created at onCreate
and destroyed at onDestroy
. and when you mark your dependencies in ActivityComponent
with @ActivityScope
its guarantees that your object will be a singleton but ActivityComponent
will be destroyed in configuration changes.
@ActivityRetainedScope
will safe guard from configuration changes such screen orientation or language changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With