13

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

Abhijith mogaveera
  • 918
  • 10
  • 16

2 Answers2

9

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.

Mehdi Yari
  • 481
  • 3
  • 12
  • First onCreate and last onDestroy of what? The Application? The Activity in question? Any Activity in the app? – phreakhead Aug 10 '21 at 01:02
  • First onCreate and last onDestroy of your activity that uses hilt for dependency injection. – Mehdi Yari Aug 10 '21 at 10:01
  • What the difference then with @ViewModelScope? – Alexander Skvortsov Sep 13 '22 at 11:29
  • Here(https://stackoverflow.com/questions/67765298/hilt-why-is-activityretainedscoped-vs-viewmodelscoped) you can read more about the difference between these two scopes. (ViewModelScoped vs ActivityRetainedScoped). @AlexanderSkvortsov – Mehdi Yari Sep 13 '22 at 15:20
4

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

Boken
  • 4,825
  • 10
  • 32
  • 42
Abhijith mogaveera
  • 918
  • 10
  • 16