LifeCycle 组件原理
image我们以V4包中的Fragment(AppCompatActivity类似)为例,看下Fragment和LifecycleOwner、LifecycleObserver、Lifecycle之间的类关系图。
- Lifecycle组件成员Lifecycle被定义成了抽象类,LifecycleOwner、LifecycleObserver被定义成了接口;
- Fragment实现了LifecycleOwner接口,该只有一个返回Lifecycle对象的方法getLifecyle();
- Fragment中getLifecycle()方法返回的是继承了抽象类Lifecycle的LifecycleRegistry。
- LifecycleRegistry中定义嵌套类ObserverWithState,该类持有GenericLifecycleObserver对象,而GenericLifecycleObserver是继承了LifecycleObserver的接口。
时序图
image-
我们在Fragment(AppCompatActivity也一样)中调用getLifecycle()方法得到LifecycleRegistry对象,然后调用addObserver()方法并将实现了LifecycleObserver接口的对象作为参数传进去。这样一个过程就完成了注册监听的过程。
-
后续就是Fragment生命周期变化时,通知LifecycleObserver的过程:Fragment的performXXX()、onXXX()方法;LifecycleRegistry的handleLifecycleEvent()方法;LifecycleObserver的onXXX()方法。
-
Fragment中performCreate()、performStart()、performResume()会先调用自身的onXXX()方法,然后再调用LifecycleRegistry的handleLifecycleEvent()方法;而在performPause()、performStop()、performDestroy()中会先LifecycleRegistry的handleLifecycleEvent()方法 ,然后调用自身的onXXX()方法
实现LifecycleObserver接口
从上面的类关系图,我们可以看到有三个接口GenericLifecycleObserver、FullLifecycleObserver、DefaultLifecycleObserver都直接或者间接继承了LifecycleObserver。然而GenericLifecycleObserver是隐藏的,我们用不了。那我们该怎么实现LifecycleObserver接口呢,有两种方式:
- 实现DefaultLifecycleObserver接口,然后重写里面生命周期方法;
public class MyText1 extends android.support.v7.widget.AppCompatTextView implements DefaultLifecycleObserver{
public MyText1(Context context) {
super(context);
}
public MyText1(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyText1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
}
}
public class MainActivity extends AppCompatActivity {
MyText1 myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (MyText1) findViewById(R.id.my_text);
getLifecycle().addObserver(myText);
}
@Override
public void onDestroy(){
super.onDestroy();
getLifecycle().removeObserver(myText);
}
}
注意:使用 DefaultLifecycleObserver ,要添加依赖 :
implementation "android.arch.lifecycle:common-java8:1.1.1"
并且 至少要求 sdk 24.
- 直接实现LifecycleObserver接口,然后通过注解的方式来接收生命周期的变化;
public class MyText extends android.support.v7.widget.AppCompatTextView implements LifecycleObserver {
public static final String TAG = MyText.class.getSimpleName();
public MyText(Context context) {
super(context);
}
public MyText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void create(){
Log.d(TAG,"ON_CREATE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.d(TAG,"ON_RESUME");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.d(TAG,"ON_PAUSE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestory(){
Log.d(TAG,"ON_DESTROY");
}
}
public class MainActivity extends AppCompatActivity {
MyText myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (MyText) findViewById(R.id.my_text);
getLifecycle().addObserver(myText);
}
@Override
public void onDestroy(){
super.onDestroy();
getLifecycle().removeObserver(myText);
}
}
Lifecycle.java文档中是建议使用第一种方式,因为文档中说明了,随着Java8成为主流,注解的方式会被弃用。
网友评论