https://developer.android.com/topic/libraries/architecture/index.html

ViewModel 有LiveData

Activity 监听(observe) LiveData, LiveData有变化通知Activity

1.ViewModel定义

Since these objects might be destroyed or re-created by the operating system, any data you hold in them will be lost. For instance, if you have a list of users in your activity, when the activity is re-created for a configuration change, the new activity will have to re-fetch the list of users. For simple data, the activity can use the onSaveInstanceState() method and restore its data from the bundle in onCreate(), but this approach is only suitable for small information like UI state, not for potentially large amounts of data like a list of users.

Another problem is that, these UI Controllers (activities, fragments, and so on) frequently need to make some asynchronous calls which may take some time to return. The UI Controller needs to manage these calls and clean them up when it is destroyed to avoid potential memory leaks. This requires a lot of maintenance, and in the case where the object is recreated for a configuration change, it is a waste of resources since it will need to re-issue the same call.

Last but not least, these UI Controllers already have a lot of responsibility to react to user actions or handle the operating system communication. When they also need to handle their resources manually, it bloats the class, creating "god activities" (or "god fragments"); that is, a single class that tries to handle all of an app's work all by itself, instead of delegating work to other classes. This also makes testing a lot harder.

It would be easier and more efficient to separate out view data ownership from UI controller logic. Lifecycles provides a new class called ViewModel, which is a helper class for the UI Controller that is responsible to prepare the data for the UI. The ViewModel is automatically retained during configuration changes so the data it holds is immediately available to the next activity or fragment instance. For the example we’ve mentioned above, it would be the ViewModel’s responsibility to acquire and keep the list of users, not the activity or the fragment.

LiveData: active inactive.

后台的时候不会更新UI,回到前台的时候,会把最新的数据更新到ui

上层不依赖下层的任何东西
ui controller 依赖view model
测试ui controller, mock view model
测试view model, mock repository
测试repository, mock data source

Android Architecture Components的更多相关文章

  1. App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南

    面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本. ...

  2. Android框架式编程之Android Architecture Components

    1. 当前Android开发面临的问题 Android开发不同于传统的桌面程序开发,桌面程序一般都有唯一的快捷方式入口,并且常作为单进程存在:而一个典型的Android应用通常由多个应用组件构成,包括 ...

  3. 安卓构架组件——概述 Android Architecture Components

    谷歌官文文档地址:https://developer.android.google.cn/topic/libraries/architecture 安卓构架组建是库的集合:帮助你设计健壮的.易测试的. ...

  4. Android Architecture Components--项目实战

    转载请注明出处,谢谢! 上个月Google Android Architecture Components 1.0稳定版发布,抽工作间隙写了个demo,仅供参考 Github地址:https://gi ...

  5. Building Maintainable Software-java篇之Couple Architecture Components Loosely

    Building Maintainable Software-java篇之Couple Architecture Components Loosely There are two ways of co ...

  6. 【Android】Architecture Components最佳实践--Lifecycles

    UI controllers (activities and fragments) 中代码越少越好,不应该自己去请求数据,而是用ViewModel来更新数据,并且监听LiveData来更新UI UI ...

  7. DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题

    使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...

  8. Android architecture

  9. Android Weekly Notes Issue #258

    Android Weekly Issue #258 May 21st, 2017 Android Weekly Issue #258 本期内容: 围绕着Google I/O的热潮, 本周的posts除 ...

随机推荐

  1. [置顶] php检测输入数据是否合法常用的类

    <?php class Fun{ function isEmpty($val) { if (!is_string($val)) return false; //是否是字符串类型 if (empt ...

  2. 出书了!实战微信小程序

    真正用心写完一本书,才知道写书真的很不容易. 我热衷喜欢分享一些技术,也喜欢钻研一些新东西,去年微信小程序刚内测的时候,我和我的同事四个人就一起研究,恰好公司有小程序相关的项目,做项目的同时,越发感觉 ...

  3. WWDC 2017, 让我们看看 iTunesConnect 有了哪些不同

    距离 WWDC 2017 过去已经有 7 天了,小伙伴们是不是已经发现我们的苹果后台和之前的界面有些略微的不同,如果有心的朋友下了 iOS 11 beta 版就会发现设备上的 App Store 界面 ...

  4. hihocoder-1483区间价值 (二分+尺取法)

    题目链接: 区间价值 给定n个数A1...An,小Ho想了解AL..AR中有多少对元素值相同.小Ho把这个数目定义为区间[L,R]的价值,用v[L,R]表示. 例如1 1 1 2 2这五个数所组成的区 ...

  5. 开发Wordpress主题时没有特色图片的功能

    在自己开发Wordpress主题的时候,发现右下方没有了之前见到的特色图片(Featured Image)功能模块 1.找到后台右上方的显示选项模块,下拉之后启用即可 2.如果以上步骤找不到该选项,那 ...

  6. 使用LNMP环境安装typecho博客的全程记录

    虽然我是搞asp.net的 但是十分欣赏php,php有很多开源的博客程序 比如大名鼎鼎的Wordpress.还有各种独立博客大牛使用的z-blog,以及短小精悍的emblog. wordpress臃 ...

  7. 让ListView中的控件失去焦点:android:descendantFocusability="blocksDescendants"

    值得注意的是,ListView中的控件不能设置clickable="true",否则会无视父控件的blockDescendants. 可参考: https://segmentfau ...

  8. bulkcopy实现批量插入与更新

    public static void UpdateData<T>(List<T> list, string TabelName) { DataTable dt = new Da ...

  9. 快排的python实现

    快排的python实现 #python 2.7 def quick_sort(L): if len(L) <= 1: return L else: return quick_sort([lt f ...

  10. NoSQL之Redis数据库初探

    一.NoSQL的风生水起 1.1 后Web2.0时代的发展要求 随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经 ...