Lifecycle Activity和Fragment生命周期感知组件 LifecycleObserver
为什么要引进 Lifecycle
使用案例
LifecycleObserver 的几种实现方式
implementation 'android.arch.lifecycle:common-java8:1.1.1'
实现 LifecycleObserver 接口
使用Observer注解的方法可以接收零个或一个参数。如果使用,则第一个参数必须是
LifecycleOwner
类型。使用
Lifecycle.Event.ON_ANY
注解的方法可以接收第二个参数,该参数必须是Lifecycle.Event
类型。提供这些附加参数是为了方便您观察多个providers和events而无需手动tracking它们。
public class BaseLifecycleObserver implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) public void onCreate() { Log.i("bqt", "Observer【onCreate】"); } @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { Log.i("bqt", "Observer【onStart】"); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume(LifecycleOwner source) { Log.i("bqt", "Observer【onResume】" + source.getLifecycle().getClass().getSimpleName()); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void onPause() { Log.i("bqt", "Observer【onPause】"); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void onStop() { Log.i("bqt", "Observer【onStop】"); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void onDestroy() { Log.i("bqt", "Observer【onDestroy】"); } @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void onAny(LifecycleOwner source, Lifecycle.Event event) { Log.i("bqt", "Observer onAny:" + event.name()); } }
实现 GenericLifecycleObserver 接口
public class BaseGenericLifecycleObserver implements GenericLifecycleObserver { @Override public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { switch (event) { case ON_CREATE: Log.i("bqt", "Observer【onCreate】"); break; case ON_START: Log.i("bqt", "Observer【onStart】"); break; case ON_RESUME: Log.i("bqt", "Observer【onResume】" + source.getLifecycle().getClass().getSimpleName()); break; case ON_PAUSE: Log.i("bqt", "Observer【onPause】"); break; case ON_STOP: Log.i("bqt", "Observer【onStop】"); break; case ON_DESTROY: Log.i("bqt", "Observer【onDestroy】"); break; case ON_ANY: Log.i("bqt", "Observer onAny"); break; default: Log.i("bqt", "Observer【不存在的事件】"); break; } } }
实现 DefaultLifecycleObserver 接口
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); }
public interface DefaultLifecycleObserver extends FullLifecycleObserver { @Override default void onCreate(@NonNull LifecycleOwner owner) {} //...其他几个方法类似 }
public class MyDefaultLifecycleObserver implements DefaultLifecycleObserver { @Override public void onCreate(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onCreate】"); } @Override public void onStart(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onStart】"); } @Override public void onResume(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onResume】" + source.getLifecycle().getClass().getSimpleName()); } @Override public void onPause(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onPause】"); } @Override public void onStop(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onStop】"); } @Override public void onDestroy(@NonNull LifecycleOwner source) { Log.i("bqt", "Observer【onDestroy】"); } }
在 Activity 中使用
public class LifecycleTestActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLifecycle().addObserver(new BaseLifecycleObserver()); //在onCreate方法中注册,以上三种使用任何一种效果都一样 ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.icon); setContentView(imageView); } }
Activity【onCreate】 Observer【onCreate】 Activity【onStart】 Observer【onStart】 Activity【onResume】 Observer【onResume】 ------------------------------ Observer【onPause】 Activity【onPause】 Observer【onStop】 Activity【onStop】 Observer【onDestroy】 Activity【onDestroy】
Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME events in this class are dispatched after the LifecycleOwner's related method returns.
Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY events in this class are dispatched before the LifecycleOwner's related method is called.
This gives you certain guarantees某些保证 on which state the owner is in.
Activity 或 Fragment 中的 performCreate()、performStart()、performResume() 方法会先调用自身的 onXXX() 方法,然后再调用 LifecycleRegistry 的 handleLifecycleEvent() 方法;
而Activity 或 Fragment 中的 performPause()、performStop()、performDestroy() 方法则会先调用 LifecycleRegistry 的 handleLifecycleEvent() 方法 ,然后再调用自身的 onXXX() 方法。
在 Fragment 中使用
public class LifecycleTestFragment extends Fragment { public LifecycleTestFragment() { getLifecycle().addObserver(new BaseGenericLifecycleObserver()); //不是在onCreate等生命周期方法中注册,而是在构造方法中就注册 } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { ImageView imageView = new ImageView(getContext()); imageView.setImageResource(R.drawable.icon); return imageView; } }
Fragment【onAttach】 Fragment【onCreate】 Observer【onCreate】 Fragment【onViewCreated】 Fragment【onActivityCreated】 Fragment【onStart】 Observer【onStart】 Fragment【onResume】 Observer【onResume】 ------------------------------ Observer【onPause】 Fragment【onPause】 Observer【onStop】 Fragment【onStop】 Fragment【onDestroyView】 Observer【onDestroy】 Fragment【onDestroy】 Fragment【onDetach】
相关的几个接口和类
LifecycleObserver
public interface LifecycleObserver {} //此接口没有声明任何方法
LifecycleOwner
public interface LifecycleOwner { Lifecycle getLifecycle(); }
Lifecycle
Lifecycle 是一个抽象类,它持有关于组件(如 Activity 或 Fragment)生命周期状态的信息,并且允许其他对象观察此状态。
LifecycleOwner 持有 Lifecycle 对象,LifecycleOwner 通过 getLifecycle() 获取内部 Lifecycle 对象,然后就可以注册或取消注册观察者 LifecycleObserver。
Lifecycle 使用两个主要枚举来跟踪其关联组件的生命周期状态
State
:当前生命周期所处状态。由框架和 Lifecycle 类所调度的生命周期事件。这些事件映射到 Activity 或 Fragment 中的回调事件。Event
:当前生命周期改变对应的事件。由 Lifecycle 对象所跟踪[tracked]
的组件的当前状态。
LifecycleRegistry
继承自 Lifecycle,是 Activity 或 Fragment 中所持有的 Lifecycle 对象的实际类型。
LifecycleRegistry 内部有一个成员变量 State 用于记录当前的生命周期,当其生命周期改变后,LifecycleRegistry 就会逐个通知每一个注册的 LifecycleObserver。
ObserverWithState:LifecycleRegistry 的内部类
static class ObserverWithState { State mState; GenericLifecycleObserver mLifecycleObserver; }