Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android

What is difference between ActivityRetainedComponent @ActivityRetainedScope and ActivityComponent @ActivityScoped in dagger hilt android

like image 631
abhijith mogaveera Avatar asked Oct 12 '20 03:10

abhijith mogaveera


People also ask

What is ActivityRetainedComponent?

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.

What is the difference between Dagger and hilt?

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.

What are hilt components?

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.

What is AndroidEntryPoint hilt?

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.

What is @activityscoped in Android with hilt?

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.

How do I retrieve a binding from an activity component?

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.

Why does the hilt module @installin(activitycomponent::class) annotate with @install in (activitycomponent)?

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.

What is the difference between dagger and hilt in Android?

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.


2 Answers

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.

like image 178
Mehdi Yari Avatar answered Oct 19 '22 12:10

Mehdi Yari


@ActivityRetainedScope will safe guard from configuration changes such screen orientation or language changes.

like image 32
abhijith mogaveera Avatar answered Oct 19 '22 11:10

abhijith mogaveera