8

I'm trying to understand why is ActivityRetainedScoped introduced for DI in Hilt. It looks to me that the scope is identical to what ViewModelScoped should do. I was under the impression that scoped worked like this:

AppScope (singleton) > ViewModelScope > ActivityScope > ViewScope > ...

But this graphic kinda hints that ViewModel and Activity scopes are... siblings?

According to the docs:

"ActivityRetainedComponent lives across configuration changes, so it is created at the first Activity#onCreate() and destroyed at the last Activity#onDestroy()."

Well, so does the view model, no? I'm pretty sure view models survive config changes (that's the whole point if having them in the first place)

What is ActivityRetainedScoped? How is it different from VM scope? Why does google likes over complicating things that should be conceptually simple

https://developer.android.com/training/dependency-injection/hilt-android

frankelot
  • 13,666
  • 16
  • 54
  • 89

1 Answers1

14

Well, even tho ActivityRetainedScope and ViewModelScope are Siblings and one could think that makes them the same, there are in fact not.

Well, so does the view model, no? I'm pretty sure view models survive config changes (that's the whole point if having them in the first place)

Well yes, but actually no. A Viewmodel does survive configuration changes, but only of its scoped lifecyleowner. So let's think of the following scenario:

You have two dependencies, one is ActivtyRetainedScoped and the other one is viewmodelscoped.

When you now inject the viewmodeldependency inside the viewmodel and the lifecycleowner of the viewmodel is an activity, then you are right, both the ActivtyRetainedScope and the ViewmodelScope would not make any difference.

But now let's assume the lifecycleowner is a fragment, in this case, the viewmodelscoped dependency would "die" when you navigate out of the fragment and the activtyretainedscope dependency would outlive the viewmodelscoped one.

I hope I could explain the difference between them both. Kinda hard with those "scopes" etc. when English is not your native language. Also, I am not 100% if this is the correct answer

Andrew
  • 4,264
  • 1
  • 21
  • 65
  • Thanks! I completely forgot about fragments (I avoid them like the plague). So you're saying that "viewmodelScope" for Fragments would be smaller that "viewModelScope" for activities. Even though they are called the same. Man google sure likes to overcomplicate things for no reason. – frankelot May 31 '21 at 12:50
  • 1
    Yes, that's what I tried to say. Yeah, googles top 3 things are: Overcomplicating, Deprecating or not delivery stuff at all. (quitted android development for that reason) – Andrew May 31 '21 at 13:35
  • +1, if they at least would take the time to clarify these ambiguities in the docs so we don't have to be guessing. I mean they are introducing a new `ActivityRetainedScoped` which clearly deserves some explaining, but nope – frankelot May 31 '21 at 15:15
  • nice explanation, it very helpful! thanks – SUPERYAO Jul 01 '21 at 10:24
  • Yeah _@ActivtyRetainedScoped acts like a shared-activity-viewModel's _@ViewModelScope which is useful sometimes - like when you need the same instance surviving config-change to be injected to any viewmodel below this activity. Think a Repo or Mediator pattern. – Alexander Skvortsov Sep 13 '22 at 14:53