activity-fragment-view的回调和日志
# activity-fragment-view的回调和日志
# activity回调日志
application.registerActivityLifecycleCallbacks(new LogActivityCallback())//每个回调里打日志即可
1
# fragment回调日志
在上面的ActivityLifecycleCallbacks里的onActivityCreated(activity,instance)回调里,
调用:
if(!(activity instanceof FragmentActivity)){
return;
}
FragmentActivity fragmentActivity = (FragmentActivity) activity;
fragmentActivity.getSupportFragmentManager().registerFragmentLifecycleCallbacks(new LogFragmentCalback(),true);
1
2
3
4
5
2
3
4
5
test editxxx
# view的生命周期:
一图流
view的onWindowFocusChanged基本上=activity的onResume 和onPause.
且onWindowFocusChanged在onDraw后执行,对于静态view,可以用来getMesuredWith()获取宽高.
当然,如果要感知外层activity或fragment的生命周期,最好还是使用lifecycle组件.
依赖
api 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
1
view上实现:
implements DefaultLifecycleObserver
1
然后愉快玩耍
编辑 (opens new window)
上次更新: 2022/12/15, 15:42:16