简体   繁体   English

何时使用ViewModel或Fragment newInstance函数?

[英]When to use ViewModel or Fragment newInstance function?

So the question is simple: When is it best to use a ViewModel and getting data from that versus using the newInstance function on a fragment and passing the data through the creation. 所以问题很简单:什么时候最好使用ViewModel并从中获取数据与在片段上使用newInstance函数并通过创建传递数据。

I've tried googling it, but there doesn't seem to be much out there. 我曾尝试使用Google搜索,但似乎没有太多。 Not anything I can easily seem to find. 我似乎不容易找到任何东西。

ViewModel together with LiveData will store the values throughout the life cycle of the Fragment/Activity. ViewModelLiveData一起将在Fragment / Activity的整个生命周期中存储值。 It can also observe the underlying data so when the ViewModel changes your views will be automatically notified and updated. 它还可以观察基础数据,因此当ViewModel更改时,视图将自动得到通知和更新。 Read more about MVVM. 了解有关MVVM的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM