【问题标题】:Avoid memory issue while using viewBinding in customView在 customView 中使用 viewBinding 时避免内存问题
【发布时间】:2021-06-14 08:00:37
【问题描述】:

基于official documentation,当我们在fragment中使用ViewBinding时,应该在fragment的onDestroyView中设置binding为null:

private var _binding:MainFragmentBinding?=null
private val binding get()=_binding!!

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

自定义视图呢?我们是否需要在 onDetachFromWindow 或任何其他函数中将绑定设置为 null?

【问题讨论】:

    标签: android kotlin memory android-custom-view android-viewbinding


    【解决方案1】:

    没有。将它放在val 中就可以了。

    fragment 的问题在于 Fragment 和它的视图有不同的生命周期,所以在 onDestroyView()onDestroy() 之间的 Fragment 中保持绑定会占用不必要的内存。

    在自定义视图中,它与存储在类属性中的任何绑定具有相同的生命周期。

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2013-08-14
      • 2016-03-29
      相关资源
      最近更新 更多