一、前言
Android Architecture Components是谷歌在Google I/O 2017大会上发布一套帮助开发者解决Android架构设计的方案。主要包括两大块内容:
(1)生命周期相关的:Lifecycle-aware Components
(2)数据库解决方案:Room
主要架构如下图:
其中有主要包含了以下几大组件:
Lifecycle、LiveData、ViewModel、Room。
下面逐一介绍每个组件:
二、Lifecycle 管理组件的生命周期
1.Lifecycle介绍
Lifecycle 是android.arch.lifecycle 包下各种类和相关接口,通过该Lifecycle 组件可以让开发者构建能够感知其他组件(主要是Activity,Fragment)的生命周期的变化。也就是说通过注册Lifecycle的类,可以监听Activity或者Fragment的生命周期的变化。
2.常规MVP的实现
在常规的MVP模式中我们监听Activity的生命周期一般是这样做的:
首先创建一个BaseActivityPresenter接口,定义相关生命周期需要回调的方法。
package com.jilian.androidarchitecture;
public interface BaseActivityPresenter {
void onCreate();
void onStart();
void onResume();
void onPause();
void onStop();
void onDestroy();
}
创建ActivityPresenter类实现BaseActivityPresenter 接口,在对应的方法中打印。
package com.jilian.androidarchitecture;
import android.util.Log;
public class ActivityPresenter implements BaseActivityPresenter {
private static final String TAG = "ActivityPresenter";
@Override
public void onCreate() {
Log.e(TAG, "onCreate: " );
}
@Override
public void onStart() {
Log.e(TAG, "onStart: " );
}
@Override
public void onResume() {
Log.e(TAG, "onResume: " );
}
@Override
public void onPause() {
Log.e(TAG, "onPause: " );
}
@Override
public void onStop() {
Log.e(TAG, "onStop: " );
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy: " );
}
}
创建需要监听的生命周期的Activity——MyActivity
package com.jilian.androidarchitecture;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class MyActivity extends AppCompatActivity {
private BaseActivityPresenter presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
presenter = new ActivityPresenter();
presenter.onCreate();
}
@Override
protected void onResume() {
super.onResume();
presenter.onResume();
}
@Override
protected void onPause() {
super.onPause();
presenter.onPause();
}
@Override
protected void onStop() {
super.onStop();
presenter.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
}
}
然后在MyActivity的各个生命周期方法中 调用presenter中的对应的方法,将数据进行打印
02-21 14:12:47.739 4328-4328/com.jilian.androidarchitecture E/ActivityPresenter: onCreate:
02-21 14:12:47.744 4328-4328/com.jilian.androidarchitecture E/ActivityPresenter: onResume:
02-21 14:12:49.488 4328-4328/com.jilian.androidarchitecture E/ActivityPresenter: onPause:
02-21 14:12:49.903 4328-4328/com.jilian.androidarchitecture E/ActivityPresenter: onStop:
02-21 14:12:49.904 4328-4328/com.jilian.androidarchitecture E/ActivityPresenter: onDestroy:
以上的做法确实可以实现对Activity的生命周期进行监听,但是当我们需要监听其他的Activity的时候,我们又需要在另外的一个Activity中的每一个生命周期方法调用presenter中的对应的方法。这样做会使得代码比较冗余。Lifecycle正是解决了该问题。
3. 使用 Lifecycle
我们创建一个ActivityLifeObserver,实现BaseActivityPresenter和LifecycleObserver接口。在对应方法中加上相应的 @OnLifecycleEvent注解,如@OnLifecycleEvent(Lifecycle.Event.ON_CREATE),就是监听Activity的onCreat方法。
package com.jilian.androidarchitecture;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.util.Log;
public class ActivityLifeObserver implements BaseActivityPresenter, LifecycleObserver {
private static final String TAG ="ActivityLifeObserver" ;
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
@Override
public void onCreate() {
Log.e(TAG, "onCreate: " );
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
@Override
public void onStart() {
Log.e(TAG, "onStart: " );
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
@Override
public void onResume() {
Log.e(TAG, "onResume: " );
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
@Override
public void onPause() {
Log.e(TAG, "onPause: " );
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
@Override
public void onStop() {
Log.e(TAG, "onStop: " );
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy: " );
}
}
创建需要监听的Activity——LifecycleActivity,实现LifecycleOwner接口。然后注册我们创建的ActivityLifeObserver。
package com.jilian.androidarchitecture;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class LifecycleActivity extends AppCompatActivity implements LifecycleOwner {
private LifecycleRegistry registry;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);
registry = new LifecycleRegistry(this);
//注册需要监听的 Observer
registry.addObserver(new ActivityLifeObserver());
}
@Override
public Lifecycle getLifecycle() {
return registry;
}
}
注册完之后就可以完美的监听LifecycleActivity 的生命周期的变化。
02-21 14:44:33.965 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onCreate:
02-21 14:44:33.966 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onStart:
02-21 14:44:33.969 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onResume:
02-21 14:44:38.895 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onPause:
02-21 14:44:39.340 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onStop:
02-21 14:44:39.341 4328-4328/com.jilian.androidarchitecture E/ActivityLifeObserver: onDestroy:
从上面我们可以看到Lifecycle主要使用步骤:
1.创建ActivityLifeObserver,并在相应的方法中添加相应的注解,表明需要监听Activity生命周期的哪一个环节。
2.在Activity中注册ActivityLifeObserver,这样Activity生命周期的变化就能被ActivityLifeObserver感知。