为整个应用程序实现一个 ViewModel
Implementing a ViewModel for entire the application
我想为整个应用程序构建一个视图模型,以便在多个片段和活动之间发送和接收数据。那么,我该怎么做呢
不要实现应用范围内的 ViewModel
来自维基百科article:
The viewmodel of MVVM is a value converter, meaning the viewmodel is responsible for exposing (converting) the data objects from the model in such a way that objects are easily managed and presented. In this respect, the viewmodel is more model than view, and handles most if not all of the view's display logic. The viewmodel may implement a mediator pattern, organizing access to the back-end logic around the set of use cases supported by the view.
来自 ViewModel
class documentation:
ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).
A ViewModel is always created in association with a scope (a fragment or an activity) and will be retained as long as the scope is alive.
ViewModel's only responsibility is to manage the data for the UI.
明确定义 ViewModel 是 View 和 Model 之间的绑定器,仅此而已。目前只有 Activity
、Fragment
和它们的子 class 实现了 ViewModelStoreOwner
接口,允许 ViewModel
作用于它们。
还 Application
范围 ViewModel
违反了重要 SOLID principle - Interface segregation (ISP)。它声明如下:
ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy.
如何在activity和fragment之间发送和接收数据?
一些比应用程序更容易沟通的方法ViewModel
:
- 活动之间 Intents
- 片段之间通过Bundle, via Navigation Component with Safe Args and via ViewModel
- 在 activity 和片段之间 Fragment.setArguments()
我想为整个应用程序构建一个视图模型,以便在多个片段和活动之间发送和接收数据。那么,我该怎么做呢
不要实现应用范围内的 ViewModel
来自维基百科article:
The viewmodel of MVVM is a value converter, meaning the viewmodel is responsible for exposing (converting) the data objects from the model in such a way that objects are easily managed and presented. In this respect, the viewmodel is more model than view, and handles most if not all of the view's display logic. The viewmodel may implement a mediator pattern, organizing access to the back-end logic around the set of use cases supported by the view.
来自 ViewModel
class documentation:
ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).
A ViewModel is always created in association with a scope (a fragment or an activity) and will be retained as long as the scope is alive.
ViewModel's only responsibility is to manage the data for the UI.
明确定义 ViewModel 是 View 和 Model 之间的绑定器,仅此而已。目前只有 Activity
、Fragment
和它们的子 class 实现了 ViewModelStoreOwner
接口,允许 ViewModel
作用于它们。
还 Application
范围 ViewModel
违反了重要 SOLID principle - Interface segregation (ISP)。它声明如下:
ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy.
如何在activity和fragment之间发送和接收数据?
一些比应用程序更容易沟通的方法ViewModel
:
- 活动之间 Intents
- 片段之间通过Bundle, via Navigation Component with Safe Args and via ViewModel
- 在 activity 和片段之间 Fragment.setArguments()