0%

Android-Jetpack组件之Lifecycle

概述

Lifecycle是Android-Jetpack中的架构组件,它具有生命周期感知能力,也就是说我们能在Activity或者Fragment的生命周期发生变化的时候得到通知。

在以往的开发中,我们经常在Activity的生命周期回调中进行各种注册解绑等,比如说EventBus的注册解绑,广播的注册解绑等。如果我们把很多这种与生命周期相关的代码都直接放在Activity的生命周期回调中,Activity将会变得难以维护。而本质上我们需要的只是Activity或者Fragment的生命周期发生改变的时候能通知我们,以便在对应生命周期中执行对应的方法,因此可以使用Lifecycle。

androix中导入依赖:

1
2
3
4
5
6
7
8
9
10
11
12
def lifecycle_version = "2.2.0"

// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

// 注解处理器:
// 1. 添加时则使用注解处理器的方式处理Lifecycle的注解
// 2. 未添加时则使用反射方式处理Lifecycle的注解
// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

生命周期

Lifecycle使用两个枚举来跟踪其关联组件的生命周期状态:

  • Event:Lifecycle类分派的生命周期事件,这些事件映射到Activity和Fragment中的回调事件。
  • State:Lifecycle对象跟踪的组件的当前状态。

如下图:

lifecycle-states

基本使用

LifecycleObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MyLifecycleObserver implements LifecycleObserver {
private static final String TAG = "Lifecycle";

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate() {
Log.d(TAG, "onCreate");
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
Log.d(TAG, "onStart");
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
Log.d(TAG, "onResume");
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
Log.d(TAG, "onPause");
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
Log.d(TAG, "onStop");
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
Log.d(TAG, "onDestroy");
}
}

// or
public class MyLifecycleObserver implements LifecycleEventObserver {
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
switch (event) {
case ON_CREATE:
register();
break;
case ON_DESTROY:
unregister();
break;
}
}

private void register() {
// ...
}

private void unregister() {
// ...
}
}

Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class MainActivity extends Activity implements LifecycleOwner {

private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getLifecycle().addObserver(new MyLifecycleObserver());
// mLifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
}

@Override
protected void onStart() {
super.onStart();
// mLifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}

@Override
protected void onResume() {
super.onResume();
// mLifecycleRegistry.setCurrentState(Lifecycle.State.RESUMED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
}

@Override
protected void onPause() {
// mLifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
super.onPause();
}

@Override
protected void onStop() {
// mLifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
super.onStop();
}

@Override
protected void onDestroy() {
// mLifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
super.onDestroy();
}

@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}

AppCompatActivity(默认实现了LifecycleOwner)

1
2
3
4
5
6
7
8
9
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLifecycle().addObserver(new MyLifecycleObserver());
}
}

Lifecycle

Lifecycle是一个抽象类,其实现类为:LifecycleRegistry。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public abstract class Lifecycle {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@NonNull
AtomicReference<Object> mInternalScopeRef = new AtomicReference<>();

@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer);

@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer);

@MainThread
@NonNull
public abstract State getCurrentState();

public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
}

public enum State {
/**
* Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch
* any more events. For instance, for an {@link android.app.Activity}, this state is reached
* <b>right before</b> Activity's {@link android.app.Activity#onDestroy() onDestroy} call.
*/
DESTROYED,

/**
* Initialized state for a LifecycleOwner. For an {@link android.app.Activity}, this is
* the state when it is constructed but has not received
* {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} yet.
*/
INITIALIZED,

/**
* Created state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached in two cases:
* <ul>
* <li>after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call;
* <li><b>right before</b> {@link android.app.Activity#onStop() onStop} call.
* </ul>
*/
CREATED,

/**
* Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached in two cases:
* <ul>
* <li>after {@link android.app.Activity#onStart() onStart} call;
* <li><b>right before</b> {@link android.app.Activity#onPause() onPause} call.
* </ul>
*/
STARTED,

/**
* Resumed state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached after {@link android.app.Activity#onResume() onResume} is called.
*/
RESUMED;

/**
* Compares if this State is greater or equal to the given {@code state}.
*
* @param state State to compare with
* @return true if this State is greater or equal to the given {@code state}
*/
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
}

Lifecycle初始化

1
2
3
4
5
6
7
8
9
public class LifecycleRegistry extends Lifecycle {
private final WeakReference<LifecycleOwner> mLifecycleOwner;
private State mState;

public LifecycleRegistry(@NonNull LifecycleOwner provider) {
mLifecycleOwner = new WeakReference<>(provider);
mState = INITIALIZED;
}
}

LifecycleOwner是一个接口:

1
2
3
4
public interface LifecycleOwner {
@NonNull
Lifecycle getLifecycle();
}

添加观察者

1
2
3
4
5
6
7
8
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap = new FastSafeIterableMap<>();

public void addObserver(@NonNull LifecycleObserver observer) {
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
// ...
}

LifecycleObserver是一个空接口:

1
2
3
public interface LifecycleObserver {

}

生命周期回调

moveToState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@MainThread
public void setCurrentState(@NonNull State state) {
moveToState(state);
}

public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
State next = getStateAfter(event);
moveToState(next);
}

static State getStateAfter(Event event) {
switch (event) {
case ON_CREATE:
case ON_STOP:
return CREATED;
case ON_START:
case ON_PAUSE:
return STARTED;
case ON_RESUME:
return RESUMED;
case ON_DESTROY:
return DESTROYED;
case ON_ANY:
break;
}
throw new IllegalArgumentException("Unexpected event value " + event);
}

moveToState 方法最后调用的是 ObserverWithState 的 dispatchEvent 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static class ObserverWithState {
State mState;
LifecycleEventObserver mLifecycleObserver;

ObserverWithState(LifecycleObserver observer, State initialState) {
mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
mState = initialState;
}

void dispatchEvent(LifecycleOwner owner, Event event) {
State newState = getStateAfter(event);
mState = min(mState, newState);
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}

这里主要看看lifecycleEventObserver方法,Lifecycle会根据不同的LifecycleObserver实例执行不同的onStateChanged逻辑。

lifecycleEventObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private static final int REFLECTIVE_CALLBACK = 1;   // 反射
private static final int GENERATED_CALLBACK = 2; // 注解处理器

static LifecycleEventObserver lifecycleEventObserver(Object object) {
// 是否是 LifecycleEventObserver/FullLifecycleObserver 的子类,可直接处理生命周期事件
boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver;
boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver;
if (isLifecycleEventObserver && isFullLifecycleObserver) {
return new FullLifecycleObserverAdapter((FullLifecycleObserver) object,
(LifecycleEventObserver) object);
}
if (isFullLifecycleObserver) {
return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null);
}

if (isLifecycleEventObserver) {
return (LifecycleEventObserver) object;
}
// 不是上面的子类,则走反射或注解逻辑,前面的实例便是如此
final Class<?> klass = object.getClass();
// 此处根据klass类去查找是否有注解处理器生成的类
int type = getObserverConstructorType(klass);
if (type == GENERATED_CALLBACK) {
// 如果注解处理器有起作用,则通过生成的GeneratedAdapter子类构造器创建实例
List<Constructor<? extends GeneratedAdapter>> constructors = sClassToAdapters.get(klass);
if (constructors.size() == 1) {
GeneratedAdapter generatedAdapter = createGeneratedAdapter(constructors.get(0), object);
return new SingleGeneratedAdapterObserver(generatedAdapter);
}
GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()];
for (int i = 0; i < constructors.size(); i++) {
adapters[i] = createGeneratedAdapter(constructors.get(i), object);
}
return new CompositeGeneratedAdaptersObserver(adapters);
}
// 使用反射方式
return new ReflectiveGenericLifecycleObserver(object);
}

GeneratedAdapter

GeneratedAdapter接口如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface GeneratedAdapter {

/**
* Called when a state transition event happens.
*
* @param source The source of the event
* @param event The event
* @param onAny approveCall onAny handlers
* @param logger if passed, used to track called methods and prevent calling the same method
* twice
*/
void callMethods(LifecycleOwner source, Lifecycle.Event event, boolean onAny, MethodCallsLogger logger);
}

注解处理器生成的GeneratedAdapter子类命名规则类似于MyLifecycleObserver_LifecycleAdapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class MyLifecycleObserver_LifecycleAdapter implements GeneratedAdapter {
final MyLifecycleObserver mReceiver;

MyLifecycleObserver_LifecycleAdapter(MyLifecycleObserver receiver) {
this.mReceiver = receiver;
}

@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
MethodCallsLogger logger) {
boolean hasLogger = logger != null;
if (onAny) {
return;
}
if (event == Lifecycle.Event.ON_CREATE) {
if (!hasLogger || logger.approveCall("onCreate", 1)) {
mReceiver.onCreate();
}
return;
}
if (event == Lifecycle.Event.ON_START) {
if (!hasLogger || logger.approveCall("onStart", 1)) {
mReceiver.onStart();
}
return;
}
if (event == Lifecycle.Event.ON_RESUME) {
if (!hasLogger || logger.approveCall("onResume", 1)) {
mReceiver.onResume();
}
return;
}
if (event == Lifecycle.Event.ON_PAUSE) {
if (!hasLogger || logger.approveCall("onPause", 1)) {
mReceiver.onPause();
}
return;
}
if (event == Lifecycle.Event.ON_STOP) {
if (!hasLogger || logger.approveCall("onStop", 1)) {
mReceiver.onStop();
}
return;
}
if (event == Lifecycle.Event.ON_DESTROY) {
if (!hasLogger || logger.approveCall("onDestroy", 1)) {
mReceiver.onDestroy();
}
return;
}
}
}

接下来介绍几个LifecycleObserver的实例。

LifecycleEventObserver

1
2
3
public interface LifecycleEventObserver extends LifecycleObserver {
void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event);
}

FullLifecycleObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface FullLifecycleObserver extends LifecycleObserver {

void onCreate(LifecycleOwner owner);

void onStart(LifecycleOwner owner);

void onResume(LifecycleOwner owner);

void onPause(LifecycleOwner owner);

void onStop(LifecycleOwner owner);

void onDestroy(LifecycleOwner owner);
}

FullLifecycleObserverAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class FullLifecycleObserverAdapter implements LifecycleEventObserver {

private final FullLifecycleObserver mFullLifecycleObserver;
private final LifecycleEventObserver mLifecycleEventObserver;

FullLifecycleObserverAdapter(FullLifecycleObserver fullLifecycleObserver,
LifecycleEventObserver lifecycleEventObserver) {
mFullLifecycleObserver = fullLifecycleObserver;
mLifecycleEventObserver = lifecycleEventObserver;
}

@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
switch (event) {
case ON_CREATE:
mFullLifecycleObserver.onCreate(source);
break;
case ON_START:
mFullLifecycleObserver.onStart(source);
break;
case ON_RESUME:
mFullLifecycleObserver.onResume(source);
break;
case ON_PAUSE:
mFullLifecycleObserver.onPause(source);
break;
case ON_STOP:
mFullLifecycleObserver.onStop(source);
break;
case ON_DESTROY:
mFullLifecycleObserver.onDestroy(source);
break;
case ON_ANY:
throw new IllegalArgumentException("ON_ANY must not been send by anybody");
}
if (mLifecycleEventObserver != null) {
mLifecycleEventObserver.onStateChanged(source, event);
}
}
}

SingleGeneratedAdapterObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SingleGeneratedAdapterObserver implements LifecycleEventObserver {

private final GeneratedAdapter mGeneratedAdapter;

SingleGeneratedAdapterObserver(GeneratedAdapter generatedAdapter) {
mGeneratedAdapter = generatedAdapter;
}

@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
mGeneratedAdapter.callMethods(source, event, false, null);
mGeneratedAdapter.callMethods(source, event, true, null);
}
}

CompositeGeneratedAdaptersObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class CompositeGeneratedAdaptersObserver implements LifecycleEventObserver {

private final GeneratedAdapter[] mGeneratedAdapters;

CompositeGeneratedAdaptersObserver(GeneratedAdapter[] generatedAdapters) {
mGeneratedAdapters = generatedAdapters;
}

@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
MethodCallsLogger logger = new MethodCallsLogger();
for (GeneratedAdapter mGenerated: mGeneratedAdapters) {
mGenerated.callMethods(source, event, false, logger);
}
for (GeneratedAdapter mGenerated: mGeneratedAdapters) {
mGenerated.callMethods(source, event, true, logger);
}
}
}

ReflectiveGenericLifecycleObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ReflectiveGenericLifecycleObserver implements LifecycleEventObserver {
private final Object mWrapped;
private final CallbackInfo mInfo;

ReflectiveGenericLifecycleObserver(Object wrapped) {
mWrapped = wrapped;
mInfo = ClassesInfoCache.sInstance.getInfo(mWrapped.getClass());
}

@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Event event) {
mInfo.invokeCallbacks(source, event, mWrapped);
}
}

因为反射会消耗性能,因此通过 ClassesInfoCache 类为 ReflectiveGenericLifecycleObserver 类要调用的各种方法的相关信息进行了缓存。

总结

Lifecycle的工作流程简要如下:

  1. 首先初始化Lifecycle实例,然后添加观察者;
  2. 当组件生命周期变化时,调用Lifecycle的方法设置状态;
  3. 根据状态获取对应的Event,然后调用观察者们通过注解注册的方法;
  4. 调用观察者们的@OnLifecycleEvent注解的方法,有三种途径:
    • 用户注册的观察者直接继承了onStateChanged方法,如:FullLifecycleObserverAdapter;
    • 用户添加了注解处理器依赖,则使用其编译期生成的类;
    • 反射,通过缓存,避免每次都通过反射来获取相关方法;

Lifecycle 通过观察者模式,封装了生命周期管理的复杂操作,开发者只需调用 getLifecycle().addObserver(...) 即可使得第三方组件在自己内部感知 LifecycleOwner 生命周期的变化,进而进行注册,解注册等处理。