android - MVVM 架构中的导航
问题描述
在使用 MVVM 架构实现的 Android 或 iOS 项目中,正确的导航方法是什么?
- ViewModel 决定导航何时发生以及哪个是目的地
- ViewModel 决定导航何时发生,View 决定哪个是目的地
- 视图决定导航何时发生以及哪个是目的地
解决方案
I believe it's supposed to be like so:
- The
ViewModel
decides when the navigation happens, the destination, and also the data, if any, to be passed to the destination. - The
View
performs the actual action, decides on the navigation animation and other UI stuff.
So ultimately we need the logic to be in the ViewModel
and the UI stuff should be the responsibility of the View
.
One way to go about it is to fire an Event
in the ViewModel
when navigation needs to happen. This event should specify the destination and data to be passed if any. The View
subscribes to this event and whenever it is fired, it decides upon the navigation animation and then performs the actual action (the actual act of navigation is a part of the UI) taking into consideration the destination and the data to be passed.
推荐阅读
- javascript - 如何将获取数据设置为innerHTML中的img和div标签
- javascript - 正则表达式检查集合的第一个和最后一个字符是否不同
- list - “循环”填充不可变列表
- python - 带有特殊字符的 AlphaNumeric 单词的正则表达式
- reactjs - 如何将 React 组件渲染到主 App 组件中/
- javascript - Rsuite SelectPicker 需要获取label的值
- ios - iOS UIScrollView 在弹出/推送时向下滚动(UIKeyboardWillChangeFrameNotification 交互触发)
- visual-studio-code - VSCode 自定义宏
- go - 如何将上下文值从 Gin 中间件传播到 gqlgen 解析器?
- css - Tailwind css - 使用 className 自定义间距值作为 tailwind 配置的变量