Android官方架构组件JetPack
使用android官方架构Jetpack一段时间 开发了2个项目 其中一个项目部分模块还使用了kotlin,曾今我一度想放弃安卓开发但是转折点就在我学习使用了jetpack,好了和大家一起看下jetpack的介绍 下图
android jetpack 在titlebar上 位置很高。第一个android studio是开发环境,google play是后台服务,接下来就是咱们的jetpack 那就是客户端开发的标准了。看jetpack的那个插图 一个机器人带喷气背包要飞起来的样子 确实 使用了这个架构及组件你会觉得飞起来一样,再也不会有想放弃的感觉。



上边是jetpack架构组件的优势 特点 下边我post一些youtube油管上关于这个架构(dataBinding 和 viewModel)的一些架构图及说明




下边是viewModel lifeCycle的一些
一:viewModel的特点是:
1,屏幕旋转时viewmodel不会销毁也就是我们的数据(本地或网络)不会因为屏幕旋转而去重新加载。
2,viewModel在按下返回键 我们手动或者系统杀掉app进程 会销毁。
二:好处是 业务与UI完全分离解耦 扩展好易维护
1,完全替换AsyncTaskLoader
2,持有数据 减少activity或者fragment里关于数据的业务代码
3,UI controller(activity fragment)展示数据即可,viewModel 持有数据。
三:liveData 的特点是 被观察者 可以监听数据的变化 并直接更新UI(databinding)
1,我们只需要关注业务就好 更新数据,UI会自动变化
2,livedata更新UI只会在started和resummed的生命周期下更新数据。
3,方便测试












下边是关于ROOM组件的一些图
使用ROOM的总结 只需要很少的代码就实现了数据库CRUD的功能 但是需要注意的一点是 @Dao注解的接口里的方法声明一定要对 否则编译不会通过,例如delete方法 返回值是void/Unit(kotlin) insert单个或者insert批量 返回值是long或者long[] kotlin是Long 和Array<Long>












