使用Lifecycle全局监听Fragment的生命周期
程序员文章站
2022-06-08 16:30:12
...
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");
}
上一篇: recyclerview实现瀑布流布局
下一篇: C# menuStrip 配置