Skip to content

Scope ViewModel to NavHost #385

@dwass

Description

@dwass

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions