首页 > 其他 > 详细

jetpack-lifecycle

时间:2021-07-15 11:45:16      阅读:24      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/--here--gold--you--want/p/15014327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!