1.得到Lifecycle对象
public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
//设置监听
this.mLifecycleOwner = lifecycleOwner;
mLifecycleOwner.getLifecycle().addObserver(this);
}
2.通过Lifecycle的对象
// 通过Lifecycle监听生命周期
//条件注解监听具体需要监听的生命周期
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
isOnResume = true;
startLoop("resume");
}
// 通过Lifecycle监听生命周期
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
isOnResume = false;
Log.d("TAG", "onPause: ");
stopLoop("pause");
}
网友评论