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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值