Properly Scoping ViewModel Across Screens

余生长醉 提交于 2021-01-05 07:20:10

问题


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:

  1. 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.

  2. Or do something like this:

which means have nested navigation over nested navigation (which I don't think is the right approach?).

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!