Last active
January 15, 2021 16:59
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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