如何删除viewTreeObserver
?有3个不同的removeOnGlobalFocusChangeListener
回调
class MyFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.root.viewTreeObserver.addOnGlobalLayoutListener {
doSomething()
}
}
override fun onDestroy() {
super.onDestroy()
// I don't know if this is the correct one to call
binding.root.viewTreeObserver.removeOnGlobalFocusChangeListener { oldFocus, newFocus -> }
}
}
1条答案
按热度按时间q5iwbnjs1#
你应该保持对这个监听器的引用(当在
onViewCreated
中创建时),并使用removeOnGlobalFocusChangeListener(..)
方法在onViewDestroyed
中释放它,并将监听器作为victim
传递。或更早,如果doSomething()
调用不需要如此频繁地调用(一次?)