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除 ...
随机推荐
- 《锋利的jQuery》读书笔记(动画)
1.show()和hide() 实质就是改变当前DOM对象的display为block.none或inline-block(取决于之前的display),如下: $("element&quo ...
- C++函数模版实现
若一个程序的功能是对某种特定的数据类型进行处理,则将所处理的数据类型说明为参数,那么就可以把这个程序改写成为模版,模版可以让程序对任何其他数据类型进行同样方式的处理. 本节主要是说一下C++的函数模版 ...
- Android快速开发-选项卡
介绍 几行代码实现Android选项卡界面,支持标准底部Tab自定义视图选项卡,头部文字选项卡. 底部自定义视图选项卡 先来看看实现下图中的效果我们代码应该怎么写? 实现上图效果只需以下代码: pub ...
- Developing on Windows Phone 8 Devices
Developing on Windows Phone 8 Deviceshttp://docs.madewithmarmalade.com/native/platformguides/wp8guid ...
- 浅谈java使用指定字符集编码,以及常见的字符集
问题的引入:在InputStreamReader(OutputStreamWriter)的构造方法中,有指定字符集编码,那么什么是字符集?有哪些常用的字符集?怎么用字符集进行编码? 一 什么是字符 ...
- AS3舞台的大小,可视区域大小及SWF文件的原始尺寸大小
AS3舞台的大小,可视区域大小及SWF文件的原始尺寸大小三者之间没有什么关系. 当前可视区域:stage.stageWidth,stage.stageHeight.SWF文件编译后的原始尺寸大小:lo ...
- 迭代器模式在 Java 容器中的实现
迭代器接口是迭代器模式实现的精髓: public interface Iterator<E> { boolean hasNext(); E next(); ... } 假设某容器名为 Xx ...
- 使用Metasploit收集邮箱信息
Metasploit提供了很多辅助的模块,非常实用.今天介绍一个叫search_email_collector的模块,它的功能是查找搜索引擎(google.bing.yahoo),收集和某个域名有关的 ...
- 如何加快MyEclipse的启动速度
学习java开发的朋友对Myeclipse应该不陌生,MyEclipse企业级工作平台(MyEclipseEnterprise Workbench ,简称MyEclipse)是对EclipseIDE的 ...
- 微软原版WINDOWS10-LTSB-X64位操作系统的全新安装与优化
原版WINDOWS10_LTSB_X64位操作系统,安装U盘的制作 1.在一台能正常运行的电脑上,下载原版WINDOWS10_LTSB_X64位操作系统镜像(ISO)文件: 2.运行UltraISO. ...