ViewBindingProvider is a library designed to help you easily integrate Android's view binding to your fragments.
It keeps track of the view binding with fragment view's lifecycle, so that you don't have to.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency
dependencies {
implementation 'com.github.jamesalee213:viewbinding-provider:1.0.5'
}
Android's view binding is great!
You no longer have to find your views or worry about casting them.
However, you have to keep track of the binding if you are using it in a fragment.
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
With this library, you no longer have to keep track of it!
Just use the ViewBindingProvider and it will keep track of the binding with the fragment's view lifecycle owner
You can look at
MainFragment
in the demo for more detailed example
private val viewBindingProvider by viewBindingProvider<ResultProfileBinding>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return viewBindingProvider.initialize {
// You still have to inflate/bind by calling the static method
ResultProfileBinding.inflate(layoutInflater, container, false)
}
}
If you decide to use this library, that is great.
But I highly recommend using Compose for your next project.