How to unbind views in Kotlin/Android?

867 Views Asked by At

How do you unbind a view in Kotlin for Android, and/or properly dispose of view elements for fragments and custom views in onDestroy/onDestroyView?

In Java/Android, you might do your cleanup in onDestroy() or onDestroyView() for fragments, setting null to the view elements or using something like Unbinder.unbind() if you were using ButterKnife.

Syntax restrictions seem to prevent setting view elements to null in onDestroyView().

private lateinit var contents: ViewGroup

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
root = inflater.inflate(R.layout.example, container, false)
contents = root.bind(R.id.contents) // calls a findViewById,   (purposefully not using the view id cache)
}

override fun onDestroyView() {
// What do I do here?
contents = null // syntax error
super.onDestroyView()
}

`

Is clean up of view elements required in Kotlin?

0

There are 0 best solutions below