Skip to content

Instantly share code, notes, and snippets.

@ch8n
Last active January 15, 2021 16:59
Show Gist options
  • Save ch8n/dfe576784a61c3c79ae292eda946f8fe to your computer and use it in GitHub Desktop.
Save ch8n/dfe576784a61c3c79ae292eda946f8fe to your computer and use it in GitHub Desktop.
Base Activity for view binding | checkout complete blog from - https://chetangupta.net/viewbinding/
// Author : ChetanGupta.net @Androidbites
abstract class ViewBindingActivity<VB : ViewBinding> : AppCompatActivity() {
private var _binding: ViewBinding? = null
abstract val bindingInflater: (LayoutInflater) -> VB
@Suppress("UNCHECKED_CAST")
protected val binding: VB
get() = _binding as VB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = bindingInflater.invoke(layoutInflater)
setContentView(requireNotNull(_binding).root)
setup()
}
abstract fun setup()
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment