转载请标明出处:http://blog.youkuaiyun.com/zhaoyanjun6/article/details/125585064
本文出自【赵彦军的博客】
文章目录
ViewTreeLifecycleOwner是什么?
ViewTreeLifecycleOwner 是Lifecycle KTX中提供的View的一个扩展方法,可以快速地获取一个最近的Fragment或者Activity的LifecycleOwner。
view.findViewTreeLifecycleOwner()

实现原理?
ComponentActivity
通过 ViewTreeLifecycleOwner.set 将当前 lifecycle 设置到decorView

Fragment
通过 ViewTreeLifecycleOwner.set 将当前 lifecycle 设置到根view

DialogFragment
通过 ViewTreeLifecycleOwner.set 将当前 lifecycle 设置到decorView

ComponentDialog
通过 ViewTreeLifecycleOwner.set 将当前 lifecycle 设置到decorView

Fragment 作为 LifecycleOwner 遇到的坑
onCreate
在 Fragment 的 onCreate 用 this 做 LifecycleOwner 没有问题。
class MyFragment : Fragment(

本文详细介绍了ViewTreeLifecycleOwner的概念及其实现原理,并探讨了在不同组件如ComponentActivity、Fragment等中如何正确使用它来避免生命周期相关的问题。
最低0.47元/天 解锁文章
1427

被折叠的 条评论
为什么被折叠?



