一、前言
1.1、Android Architecture Components 介绍
Android Architecture Components 是谷歌在Google I/O 2017发布一套帮助开发者解决Android 架构设计的方案。里面包含了两大块内容:
-
生命周期相关的 Lifecycle-aware Components
-
数据库解决方案 Room
1.2、组件功能
官方给予 Google 组件的功能:A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence。
使用Google 提供的处理数据持久化和管理组件生命周期的类,有助于应用开发者们构建更加鲁棒性,可测的,稳定可靠的应用。
提供主要的组件有:
-
Lifecycle:管理组件生命周期
-
Room: 持久化数据结构
1.3、主要架构
<figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption>
1.4、使用组件
在项目根目录 build.gradle 文件添加仓库依赖:
allprojects { repositories { jcenter() google() }}
如果遇到如下因 gradle 版本导致的编译失败问题:
Error:(6, 1) A problem occurred evaluating root project 'TestArc'.>Could not find method google() for arguments [] on repository container;
可修改为:
maven { url 'https://maven.google.com'}
然后在主 module 的 build.gradle 文件添加需要依赖的组件:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' def lifecycle_version = "1.1.1" // ViewModel and LiveData compile "android.arch.lifecycle:extensions:$lifecycle_version" // alternatively - just ViewModel compile "android.arch.lifecycle:viewmodel:$lifecycle_version" // use -ktx for Kotlin // alternatively - just LiveData compile "android.arch.lifecycle:livedata:$lifecycle_version" // alternatively - Lifecycles only (no ViewModel or LiveData). // Support library depends on this lightweight import compile "android.arch.lifecycle:runtime:$lifecycle_version" annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // alternately - if using Java8, use the following instead of compiler compile "android.arch.lifecycle:common-java8:$lifecycle_version" // optional - ReactiveStreams support for LiveData compile "android.arch.lifecycle:reactivestreams:$lifecycle_version" // optional - Test helpers for LiveData // compile "android.arch.core:core-testing:$lifecycle_version" compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.facebook.stetho:stetho:1.3.1' // room compile 'android.arch.persistence.room:runtime:1.1.0' annotationProcessor 'android.arch.persistence.room:compiler:1.1.0' compile "android.arch.persistence.room:rxjava2:1.1.0"}
二、Lifecycle 管理生命周期
2.1、Lifecycle 介绍
Lifecycle 组件指的是 android.arch.lifecycle 包下提供的各种类与接口,可以让开发者构建能感知其他组件(主要指Activity 、Fragment)生命周期(lifecycle-aware)的类。
2.2、常规 MVP Presenter 使用
比如我们需要监听某个 Activity 生命周期的变化,在生命周期改变的时候打印日志,一般做法构造回调的方式,先定义基础 BaseActivityPresenter 接口:
public interface BaseActivityPresenter extends BasePresenter{ void onCreate(); void onStart(); void onResume(); void onPause(); void onStop(); void onDestroy();}
在实现类中增加自定义操作(打印日志):
public class ActivityPresenter implements BaseActivityPresenter { private static String TAG = ActivityPresenter.class.getSimpleName(); @Override public void onCreate() { LogUtil.i(TAG, "onCreate()"); } @Override public void onStart() { LogUtil.i(TAG, "onStart()"); } @Override public void onResume() { LogUtil.i(TAG, "onResume()"); } @Override public void onPause() { LogUtil.i(TAG, "onPause()"); } @Override public vo