https://kotlinlang.org logo
#compose
Title
# compose
a

Archie

07/26/2020, 12:31 PM
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

Manuel Wrage

07/26/2020, 12:32 PM
You can provide your own
ViewModelStoreOwner
via
ViewModelStoreOwnerAmbient
.
a

andylamax

07/26/2020, 12:45 PM
Link to this please? This is knew news to me
a

Andrey Kulikov

07/26/2020, 12:54 PM
it is already scoped to the activity, or fragment if you compose into the view added to fragment
a

Archie

07/26/2020, 1:29 PM
Yes, but going compose means no
Activity
or
Fragment
right?
h

Halil Ozercan

07/26/2020, 2:25 PM
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

Archie

07/26/2020, 2:48 PM
Is
SharedFlow
already available?
z

Zach Klippenstein (he/him) [MOD]

07/26/2020, 3:32 PM