So I found that you could create a `ViewModel` in ...
# compose
a
So I found that you could create a
ViewModel
in
Compose
using
viewModel()
composable function. I was wondering how to scope these
ViewModel
similar to how
ViewModel
could be scoped to
Activity
/
Fragment
or
NavGraph
? Is this possible to do in
Compose
?
m
You can provide your own
ViewModelStoreOwner
via
ViewModelStoreOwnerAmbient
.
a
Link to this please? This is knew news to me
a
it is already scoped to the activity, or fragment if you compose into the view added to fragment
a
Yes, but going compose means no
Activity
or
Fragment
right?
h
well Activities aren't going anywhere and fragments are probably going to be around for foreseeable future. It is not yet clear how the old relation between ViewModels and LifecycleOwner classes(Activity, Fragment) will transition into Compose. Currently. there seems to be no change. There is
viewModel()
composable that connects these worlds but IMO it is far more useful for transitioning to compose, rather than keep using ViewModels as the default state management. Of course, it depends on a lot of factors and in the end ViewModels can stay around thanks to the introduction of
StateFlow
,
SharedFlow
. I found that
Compose-Hilt-ViewModel
trio play well together in small examples.
a
Is
SharedFlow
already available?
z