jetpack-lifecycle

1:基本概念

  1. 对于自己自定义的一些组件,想要它们在fragment或activity中随fragment或acitivity同生同死,避免手动在onCreate或onDestroy里创建和销毁,可以令自己的组件继承LifecycleObserver接口, 然后在fragment或activity中addObserver(LifecycleObserver)
  2. LifecycleObserver没有具体要实现的方法,只需要在自己实现的继承lifecycleObserver的类中的方法上加上注释,例如
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)//绑定fragment的创建时刻
    
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)//绑定fragment的销毁时刻

    其他可参考:

    public enum Event {
            /**
             * Constant for onCreate event of the {@link LifecycleOwner}.
             */
            ON_CREATE,
            /**
             * Constant for onStart event of the {@link LifecycleOwner}.
             */
            ON_START,
            /**
             * Constant for onResume event of the {@link LifecycleOwner}.
             */
            ON_RESUME,
            /**
             * Constant for onPause event of the {@link LifecycleOwner}.
             */
            ON_PAUSE,
            /**
             * Constant for onStop event of the {@link LifecycleOwner}.
             */
            ON_STOP,
            /**
             * Constant for onDestroy event of the {@link LifecycleOwner}.
             */
            ON_DESTROY,
            /**
             * An {@link Event Event} constant that can be used to match all events.
             */
            ON_ANY;

     

2:

jetpack-lifecycle

上一篇:力扣-1818 绝对差值和(2021.07.15)


下一篇:NFS简介