PreCompose icon indicating copy to clipboard operation
PreCompose copied to clipboard

Scope ViewModel to NavHost

Open dwass opened this issue 1 year ago • 1 comments

I would like to scope a ViewModel to a NavHost (NavGraph). I have a series of screens (scenes), which are used like a wizard, to walk the user through a process of providing a bunch of input fields. I would like to create a single ViewModel that can capture all of this data, as I think having separate ViewModels for each of these screens is a) overkill and b) ties the ViewModel to closely to the specific View implementation (It should be possible, on larger screens for example, to have all the input collected in a single screen instead of providing the wizard-like multi-screen approach).

At the moment, the ViewModel is created where the (nested) NavHost is located and passed around to all the other screens. I would prefer to have each View "get" the ViewModel using a shared-ViewModel concept so I don't need to pass the ViewModel around. If the ViewModel could be scoped to the NavHost (NavGraph), then this should be possible, I just don't know how to do it.

Is this possible? If so, please explain how.

dwass avatar Sep 30 '24 13:09 dwass

Currently ViewModel can not scoped to an NavHost, so the best way is to passing a ViewModel in a Nested NavHost, just like what you do right now.

Tlaster avatar Oct 01 '24 06:10 Tlaster