需要监控生命周期类需要实现LifecycleObserver
把需要检测的生命周期方法添加相应的注解 方法名随意 注解正确就可以
class MyObserver:LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { Log.d("VideoView", "生命周期检测======ON_RESUME") } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { Log.d("VideoView", "生命周期检测======ON_PAUSE") } }
然后在需要监听的activity/fragment 添加lifecycle().addObserver
lifecycle().addObserver(MyObserver())