Skip to content

Instantly share code, notes, and snippets.

@seanghay
Created October 25, 2020 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanghay/f419deb2202560f298fad90fae999cc9 to your computer and use it in GitHub Desktop.
Save seanghay/f419deb2202560f298fad90fae999cc9 to your computer and use it in GitHub Desktop.
ViewBinding inside Fragment
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding: FragmentHomeBinding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
// calling this to avoid memory leak.
_binding = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment