Android Architecture Components
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的更多相关文章
- App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南
面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本. ...
- Android框架式编程之Android Architecture Components
1. 当前Android开发面临的问题 Android开发不同于传统的桌面程序开发,桌面程序一般都有唯一的快捷方式入口,并且常作为单进程存在:而一个典型的Android应用通常由多个应用组件构成,包括 ...
- 安卓构架组件——概述 Android Architecture Components
谷歌官文文档地址:https://developer.android.google.cn/topic/libraries/architecture 安卓构架组建是库的集合:帮助你设计健壮的.易测试的. ...
- Android Architecture Components--项目实战
转载请注明出处,谢谢! 上个月Google Android Architecture Components 1.0稳定版发布,抽工作间隙写了个demo,仅供参考 Github地址:https://gi ...
- Building Maintainable Software-java篇之Couple Architecture Components Loosely
Building Maintainable Software-java篇之Couple Architecture Components Loosely There are two ways of co ...
- 【Android】Architecture Components最佳实践--Lifecycles
UI controllers (activities and fragments) 中代码越少越好,不应该自己去请求数据,而是用ViewModel来更新数据,并且监听LiveData来更新UI UI ...
- DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题
使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...
- Android architecture
- Android Weekly Notes Issue #258
Android Weekly Issue #258 May 21st, 2017 Android Weekly Issue #258 本期内容: 围绕着Google I/O的热潮, 本周的posts除 ...
随机推荐
- New Concept English three (40)
23w/m 48 errors It has never been explained why university students seem to enjoy practical jokes mo ...
- 【python】python内存管理摘要
a = 1 id(a) == id(1) 每次退出ipython重新进入,这个Id都会不一样 sys.getrefcount(a) 可以计数某个对象的引用次数,是原来的次数+1 垃圾回收 使用gc包 ...
- *Fragment与Activity生命周期的协调
本文出处:http://blog.csdn.net/think_soft/article/details/7272869 拥有Fragment的Activity的生命周期直接影响了其中的Fragmen ...
- FlycoTabLayout 从头到脚
简介 FlycoTabLayout,是一个比Google原生TabLayout 功能更强大的TabLayout库.目前有3种TabLayout: SlidingTabLayout CommonTabL ...
- canvas 绘制验证码
注意: 真正项目中验证码图片都是由服务器端(PHP.JSP.Node.js)技术来生成. 最终效果: 代码: <!DOCTYPE html> <html> <head l ...
- [面试时]我是如何讲清楚TCP/IP是如何实现可靠传输的 转
[面试时]我是如何讲清楚TCP/IP是如何实现可靠传输的 - shawjan的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/shawjan/article/det ...
- 利用aop完成功能权限验证遇到的问题
报错信息如上,找不到此方法原因是services层的有的方法带了parameters! 解决:注解解析器这里的代码不变: 将得到的service层的class遍历所有方法(存在效率问题) 匹配该方法 ...
- linux之文件操作
1. 文件操作思维导图 2. linux系统目录结构及简单说明 linux目录图: root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存储普 ...
- intellij idea 如何更改比编辑器文本字体和大小
换上了intellij idea之后,第一件事就是想要改变下文字字体,因为在我这个27寸的2k分辨率的屏幕上,文字显然太小了. intellij idea字体设值分成两部分,一部分是UI部分字体字号设 ...
- Linux 绑定双网卡
1.立即关闭iptables并禁用开机自启动[root@mysql01 ~]# /etc/init.d/iptables stop[root@mysql01 ~]# chkconfig iptable ...