Instantiate ViewModel with composable scope

186 Views Asked by At

I want to instantiate a ViewModel with scope of a Composable function.

How can I do this?

I don't want to use navigation-compose to take advantage of its BackStackEntry as ViewModelStoreOwner.

1

There are 1 best solutions below

0
On

I faced this issue recently, the only I could solve it by instantiating it manually using remember, not that perfect but it solved my use case for now.

To do the same, you can do something like:

val composeScopeViewModel = remember {
    ResidentDetailsViewModel(
        // params if any
    )
}