beigirad
beigirad

Reputation: 5734

Instantiate ViewModel with composable scope

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.

Upvotes: 0

Views: 268

Answers (1)

Mustafa Ibrahim
Mustafa Ibrahim

Reputation: 606

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
    )
}

Upvotes: 0

Related Questions