-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fragment生命周期监听方法不正确 #15
Labels
bug
Something isn't working
Comments
非常感谢, 你是对的, view 生命周期比 fragment 周期短,也确实存在多种情况, view 被销毁了,而fragment 依然存在,也可能会出现造成多个 Observer 的情况,而 ViewBinding 和 DataBinding 都是 View 相关的,应该使用 viewLifecycleOwner, 我会在下一个版本一起修复这个问题,再次感谢 |
我指的不是 binding 中添加 addObserver, 而是 fragment.lifecycle 和 fragment.viewLifecycleOwner 可能是会存在多个的, |
hi-dhl
added a commit
that referenced
this issue
Jan 25, 2021
非常感谢你的提交和建议,这个问题在 1.0.9 上修复了 |
用 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
当前的监听方法通过fragment.lifecycle添加了Observer,
fragment.lifecycle关联的是fragment的生命周期,而非fragment.view的生命周期,
由于fragment.view活跃的生命周期存在明显比fragment活跃生命周期短的情况,
(如采用Navigation库管理fragment(attach/detach))
正确的做法是采用fragment.viewLifecycleOwner.lifecycle添加Observer。
Simple one-liner ViewBinding in Fragments and Activities with Kotlin
中fragment生命周期监听的方法是正确的
(其中onDestroy回调与fragment.onDestroyView()回调对应)
PS: observers可以不额外手动remove,因为fragment.lifecycle和LiveData都在DESTROYED时做了反注册。
The text was updated successfully, but these errors were encountered: