Android: ViewBinding in Fragment

PHOTO EMBED

Wed Feb 03 2021 13:29:33 GMT+0000 (Coordinated Universal Time)

Saved by @samp #kotlin #android

// MyFragment.kt

class MyFragment : Fragment(R.layout.fragment_my) {
  
  private var _binding: MyFragmentBinding? = null
  private val binding get() = _binding!!

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    _binding = MyFragmentBinding.bind(view)
    
    binding.someview.text = "bla bla"
  }

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