Lifecycle

为什么需要Lifecycle组件,这些就不具体细讲了。网上很多关于该类文章。参考Android 架构组件(一)——Lifecycle。下面主要描述无法找到类DefaultLifecycleObserver的情况(SDK29)。
此时我们查看一下Lifecycle包的情况。
在这里插入图片描述
发现的确没有DefaultLifecycleObserver的class或者interface。那么如果我们想实现DefaultLifecycleObserver的效果该怎么做呢?代码如下。

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
class LifecycleDeno implements LifecycleObserver {
        private static final String TAG = "LifecycleDeno";

        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        public void onCreate(LifecycleOwner owner) {
            Log.d(TAG, "onCreate: ");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onStart(LifecycleOwner owner) {
            Log.d(TAG, "onStart: ");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        public void onResume(LifecycleOwner owner) {
            Log.d(TAG, "onResume: ");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        public void onPause(LifecycleOwner owner) {
            Log.d(TAG, "onPause: ");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onStop(LifecycleOwner owner) {
            Log.d(TAG, "onStop: ");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        public void onDestroy(LifecycleOwner owner) {
            Log.d(TAG, "onDestroy: ");
        }

    }

在这里插入图片描述

此时用户可能会觉得奇怪@OnLifecycleEvent是什么东西呢?其实这就是Java注解类@interface的用法我们将在下一章中讲解。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface OnLifecycleEvent {
    Lifecycle.Event value();
}

参考文章:https://www.jianshu.com/p/22295c0a48ee

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值