Archie

    Archie

    2 years ago
    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
    ?
    Manuel Wrage

    Manuel Wrage

    2 years ago
    You can provide your own
    ViewModelStoreOwner
    via
    ViewModelStoreOwnerAmbient
    .
    andylamax

    andylamax

    2 years ago
    Link to this please? This is knew news to me
    Andrey Kulikov

    Andrey Kulikov

    2 years ago
    it is already scoped to the activity, or fragment if you compose into the view added to fragment
    Archie

    Archie

    2 years ago
    Yes, but going compose means no
    Activity
    or
    Fragment
    right?
    Halil Ozercan

    Halil Ozercan

    2 years ago
    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.
    Archie

    Archie

    2 years ago
    Is
    SharedFlow
    already available?
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago