问题
Let's say we have a signup form which span multiple screens such as:
So I created a Scoped ViewModel spanning across these screens:
The problem is that since the state is scoped across screens, going:
PersonalInfo Screen -> Set Username Screen -> (pressed back) Personal Info Screen
make it like this:
Notice that the username
state is retained (since the ViewModel is scoped across the whole flow). So when the user goes back to Set Username Screen
instead of a blank state, it retains the previous state. (But I need it to be cleared)
So what I really need is either:
Be able to know if the before screen gets disposed completely (not because of device configuration changes or it going to the backstack), so I could manually clear the state.
Or do something like this:
which means have nested navigation over nested navigation (which I don't think is the right approach?).
- Another choice is to have the ViewModel scoped only with the screen, and pass the state across screens during navigation, but I find that very tedious to do specially when there is alot of state to pass across multiple screens.
Am I missing something? Is there a suggested way to do this?
来源:https://stackoverflow.com/questions/65075735/properly-scoping-viewmodel-across-screens