[英]Avoid memory issue while using viewBinding in customView
根據官方文檔,當我們在 Fragment 中使用 ViewBinding 時,我們應該在 Fragment 的onDestroyView
中設置綁定到 null:
private var _binding:MainFragmentBinding?=null
private val binding get()=_binding!!
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
自定義視圖呢? 我們是否需要在onDetachFromWindow
或任何其他 function 中設置綁定到 null?
不,只要將它放在val
中就可以了。
Fragment 的問題在於 Fragment 及其視圖具有不同的生命周期,因此在onDestroyView()
和onDestroy()
之間保持 Fragment 中的綁定使用了不必要的 memory。
在自定義視圖中,它與存儲在 class 屬性中的任何綁定具有相同的生命周期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.