Lifecycle组件

前言

Lifecycle作为Android Jetpack架构组件。它的作用就是提供监听Activity、Fragment生命周期的方法,便于解耦。

在一些与生命周期相关的场景非常实用:
1、Dialog需要监听Activity生命周期,在onResume时刷新数据。
2、一些计时器在onPause时暂停计时,onResume时恢复计时。
3、视图控件延迟初始化,在首次onResume时才开始渲染。

一个计数器的示例

源码地址

https://github.com/PortgasAce/LifecycleDemo

没有使用Lifecycle

MainActivity中mCounterTv显示计数,mCounterDelegate在onResume后开始计数,onPause时停止。

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 AppCompatActivity {

private TextView mCounterTv;
private CounterDelegate mCounterDelegate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCounterTv = findViewById(R.id.counter);
mCounterDelegate = new CounterDelegate(mCounterTv);
}

@Override
protected void onResume() {
super.onResume();
mCounterDelegate.onResume();
}

@Override
protected void onPause() {
super.onPause();
mCounterDelegate.onPause();
}
}

public class CounterDelegate {

private TextView mCounterTv;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
int count = 0;

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
count++;
mCounterTv.setText(count + "");
sendEmptyMessageDelayed(1, 1000);
}
};

public CounterDelegate(TextView counterTv) {
mCounterTv = counterTv;
}

public void onResume() {
mHandler.sendEmptyMessageDelayed(1, 1000);
}

public void onPause() {
mHandler.removeCallbacksAndMessages(null);
}

}

使用Lifecycle

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
public class MainActivity extends AppCompatActivity {

private TextView mCounterTv;
private CounterDelegate mCounterDelegate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCounterTv = findViewById(R.id.counter);
mCounterDelegate = new CounterDelegate(mCounterTv);
getLifecycle().addObserver(mCounterDelegate);
}

@Override
protected void onDestroy() {
super.onDestroy();
getLifecycle().removeObserver(mCounterDelegate);
}
}

public class CounterDelegate implements LifecycleObserver {

private TextView mCounterTv;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
int count = 0;

@SuppressLint("SetTextI18n")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
count++;
mCounterTv.setText(String.valueOf(count));
sendEmptyMessageDelayed(1, 1000);
}
};

public CounterDelegate(TextView counterTv) {
mCounterTv = counterTv;
}

@OnLifecycleEvent(Event.ON_RESUME)
public void onResume() {
mHandler.sendEmptyMessageDelayed(1, 1000);
}

@OnLifecycleEvent(Event.ON_PAUSE)
public void onPause() {
mHandler.removeCallbacksAndMessages(null);
}

}

可以看到使用了Lifecycle组件以后不需要在Activity的生命周期内显示调用观察者的方法。只需要注册和反注册Lifecycle的监听。

Lifecycle#addObserver

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

1、同一个Observer可重复addObserver
2、addObserver会根据当前状态,立刻回调部分Event

关于第二点,具体说明一下。Lifecycle#addObserver用于设置生命周期观察者,调用该方法会立刻回调当前状态(Lifecycle#State)的Event事件。例如:

情况1:在Activity的onResume中调用Lifecycle#addObserver
Activity(LifecycleOwner)的状态为RESUMED,所以会回调Event.ON_CREATEEvent.ON_STARTEvent.ON_RESUME

情况2:在Activity的onPause中调用Lifecycle#addObserver
Activity(LifecycleOwner)的状态为STARTED,所以会回调Event.ON_CREATEEvent.ON_START,不会回调Event.ON_PAUSE!!!

Lifecycle如何监听生命周期

1、ProcessLifecycleOwnerInitializer继承自provider,在application初始化时触发其OnCreate()生命周期。
2、OnCreate中初始化LifecycleDispatcher,LifecycleDispatcher获取Application注册了名为DispatcherActivityCallback的ActivityLifecycleCallbacks。
3、DispatcherActivityCallback监听onActivityCreated,在Activity onCreate时给Activity添加一个名为ReportFragment的Fragment。
4、ReportFragment作为Fragment,能直接监听Activity生命周期,在相应的生命周期分发事件。