生命周期组件Lifecycle主要成员
- LifecycleOwner 生命周期拥有者
- LifecycleRegistry 生命周期发布者,流转事件
- LifecycleObserver 生命周期事件监听者
通过生命周期成员自己时间生命周期的监听和通知
- 生命周期持有者
/**
* 自定义view,生命周期持有者
*/
class LifecycleSub : LinearLayout, LifecycleOwner {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
private val mLifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle(): Lifecycle = mLifecycleRegistry
override fun onFinishInflate() {
super.onFinishInflate()
observer(ObserverSub())
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
override fun onWindowVisibilityChanged(visibility: Int) {
super.onWindowVisibilityChanged(visibility)
if (visibility == VISIBLE) {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
} else {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
private fun observer(observer: LifecycleEventObserver) {
removeObserver(observer)
lifecycle.addObserver(observer)
}
private fun removeObserver(observer: LifecycleEventObserver) {
lifecycle.removeObserver(observer)
}
}
- 在LifecycleOwner里面注册生命周期事件的发布者
override fun getLifecycle(): Lifecycle = mLifecycleRegistry
- 注册生命周期的观察者
先添加观察者observer(ObserverSub())
/**
* 生命周期观察者
*/
class ObserverSub: LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
Log.d("ObserverSub", "onStateChanged: ${event.targetState}")
}
}
- 打印的日志
2022-08-16 14:57:19.569 D/ObserverSub: onStateChanged: STARTED
2022-08-16 14:57:19.569 D/ObserverSub: onStateChanged: CREATED
2022-08-16 14:57:19.589 D/ObserverSub: onStateChanged: DESTROYED
2022-08-16 14:57:24.002 D/ObserverSub: onStateChanged: CREATED
2022-08-16 14:57:24.002 D/ObserverSub: onStateChanged: STARTED
2022-08-16 14:57:24.002 D/ObserverSub: onStateChanged: RESUMED
2022-08-16 15:01:09.753 D/ObserverSub: onStateChanged: STARTED
2022-08-16 15:01:09.753 D/ObserverSub: onStateChanged: CREATED
2022-08-16 15:01:09.771 D/ObserverSub: onStateChanged: DESTROYED
- 用于测试的代码,在activity中添加自定义控件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.fow.lifecyclerdemo.text.LifecycleSub
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
上面5步就可以简单实现发布和监听,在新版本中Activity/Fragment都默认实现了LifecycleOwner接口
public class AppCompatActivity extends FragmentActivity implements AppCompatCallback
public class FragmentActivity extends ComponentActivity implements
ActivityCompat.OnRequestPermissionsResultCallback,
ActivityCompat.RequestPermissionsRequestCodeValidator {
//...
//注册了LifecycleRegistry,ComponentActivity中实现了LifecycleOwner
final LifecycleRegistry mFragmentLifecycleRegistry = new LifecycleRegistry(this);
//...
//生命周期的流转
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mFragments.dispatchCreate();
}
//...
}
在FragmentActivity中根据activity的生命周期流转Lifecycle的事件,只要向LifecycleRegistry添加观察者就会收到生命周期事件,例如:LiveData