Skip to content

Instantly share code, notes, and snippets.

@gmk57
Last active November 27, 2024 00:42
Show Gist options
  • Save gmk57/aefa53e9736d4d4fb2284596fb62710d to your computer and use it in GitHub Desktop.
Save gmk57/aefa53e9736d4d4fb2284596fb62710d to your computer and use it in GitHub Desktop.
Kotlin delegates for Android View Binding with usage examples
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
/** Activity binding delegate, may be used since onCreate up to onDestroy (inclusive) */
inline fun <T : ViewBinding> AppCompatActivity.viewBinding(crossinline factory: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
factory(layoutInflater)
}
/** Fragment binding delegate, may be used since onViewCreated up to onDestroyView (inclusive) */
fun <T : ViewBinding> Fragment.viewBinding(factory: (View) -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
private var binding: T? = null
override fun getValue(thisRef: Fragment, property: KProperty<*>): T =
binding ?: factory(requireView()).also {
// if binding is accessed after Lifecycle is DESTROYED, create new instance, but don't cache it
if (viewLifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
viewLifecycleOwner.lifecycle.addObserver(this)
binding = it
}
}
override fun onDestroy(owner: LifecycleOwner) {
binding = null
}
}
/** Binding delegate for DialogFragments implementing onCreateDialog (like Activities, they don't
* have a separate view lifecycle), may be used since onCreateDialog up to onDestroy (inclusive) */
inline fun <T : ViewBinding> DialogFragment.viewBinding(crossinline factory: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
factory(layoutInflater)
}
/** Not really a delegate, just a small helper for RecyclerView.ViewHolders */
inline fun <T : ViewBinding> ViewGroup.viewBinding(factory: (LayoutInflater, ViewGroup, Boolean) -> T) =
factory(LayoutInflater.from(context), this, false)
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.button.text = "Bound!"
}
}
// Don't forget to pass layoutId in Fragment constructor
class RegularFragment : Fragment(R.layout.fragment) {
private val binding by viewBinding(FragmentBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.text = "Bound!"
}
}
// DialogFragment with onCreateDialog doesn't have a view lifecycle, so we need a different delegate
class DialogFragment1 : DialogFragment() {
private val binding by viewBinding(FragmentBinding::inflate)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding.button.text = "Bound!"
return AlertDialog.Builder(requireContext()).setView(binding.root).create()
}
}
// For DialogFragment with full-blown view we can use a regular Fragment delegate (actually the
// whole code here is exactly the same as in RegularFragment)
// NB: Constructor with layoutId was only recently added (in Fragment 1.3.0)
class DialogFragment2 : DialogFragment(R.layout.fragment) {
private val binding by viewBinding(FragmentBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.text = "Bound!"
}
}
// For RecyclerView we don't need any delegates, just a property.
// Unfortunately, here we have a name overloading: View Binding vs "binding" holder to data (onBindViewHolder).
// ViewGroup.viewBinding() helper function can reduce boilerplate a little.
class Adapter1 : ListAdapter<String, Adapter1.Holder>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
return Holder(parent.viewBinding(ListItemBinding::inflate))
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.binding.textView.text = getItem(position)
}
class Holder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root)
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
// Alternatively, we can use generic BoundHolder for all Adapters
class Adapter2 : ListAdapter<String, BoundHolder<ListItemBinding>>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoundHolder<ListItemBinding> {
return BoundHolder(parent.viewBinding(ListItemBinding::inflate))
}
override fun onBindViewHolder(holder: BoundHolder<ListItemBinding>, position: Int) {
holder.binding.textView.text = getItem(position)
}
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
open class BoundHolder<T : ViewBinding>(val binding: T) : RecyclerView.ViewHolder(binding.root)
// Personally, I prefer to encapsulate view creation & manipulation inside ViewHolder.
// In this case BoundHolder can be used as a superclass.
class Adapter3 : ListAdapter<String, Adapter3.Holder>(Differ()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = Holder(parent)
override fun onBindViewHolder(holder: Holder, position: Int) = holder.bind(getItem(position))
class Holder(parent: ViewGroup) : BoundHolder<ListItemBinding>(parent.viewBinding(ListItemBinding::inflate)) {
fun bind(item: String) {
binding.textView.text = item
}
}
private class Differ : DiffUtil.ItemCallback<String>() { ... }
}
abstract class BoundHolder<T : ViewBinding>(protected val binding: T) : RecyclerView.ViewHolder(binding.root)
@gmk57
Copy link
Author

gmk57 commented Jan 20, 2021

Here is my take on Kotlin delegates to make Android View Binding a bit nicer to use (yes, I miss synthetics too ;). Looking at several existing solutions, mainly by Gabor Varadi, Kirill Rozov, Jamie Sanson and Eugeny Babich, I tried to build the simplest, yet correct, code for typical use cases.

If DefaultLifecycleObserver is not resolved, you may need to add its dependency, e.g. androidx.lifecycle:lifecycle-common-java8:2.3.0

Unfortunately, DefaultLifecycleObserver.onDestroy() is called before Fragment.onDestroyView(), so the cached value is lost by that time. If you really need to access binding from onDestroyView, delegate does the best it can: creates new binding, but does not cache it. It works, but is somewhat wasteful. I think it's still better than to throw exception, have bugs in edge cases or just cache stale binding indefinitely.

Another thing that bothers me a little is that for Fragment we have to specify layout twice: in constructor (R.layout.some_fragment) and in binding definition (SomeFragmentBinding::bind), which seems error-prone. I would prefer to use inflate(), as in Activity, but can't find a good way to pass parent/container to delegate.

I'm not sure if it is a good practice to have four different functions named viewBinding, but since they have different receivers and/or arguments (so the compiler doesn't get confused), and they are all directly related to accessing View Binding, it doesn't bother me much. Of course you can give them different names if you so prefer.

The code is not thread-safe, because Android UI is not thread-safe anyway.

I've tested it as much as I could, including back stack scenario, with Fragments 1.2.5 & 1.3.0. If you think something is incorrect or not optimal, please drop a line.

@Zhuinden
Copy link

If you really need to access binding from onDestroyView

Then register a LifecycleObserver from onViewCreated. 😉

@gmk57
Copy link
Author

gmk57 commented Jan 22, 2021

Then register a LifecycleObserver from onViewCreated.

@Zhuinden This is what happens in practice: the first event where binding can be safely accessed is onViewCreated(), and then my LifecycleObserver is registered.
Or do you mean that anybody who wants to clean up e.g. RecyclerView adapter should implement a separate LifecycleObserver in it? Yes, it should work, but it means additional code to write (and test). Technically findViewById() is a valid option too. ;)

@Zhuinden
Copy link

Or do you mean that anybody who wants to clean up e.g. RecyclerView adapter should implement a separate LifecycleObserver in it? Yes, it should work

Yes

but it means additional code to write

That's how people get things done, so I don't see the problem with it 😂

@StelianMorariu
Copy link

StelianMorariu commented Jan 29, 2021

Android Lint fails if I use the viewBinding delegate for Activity 😢

@gmk57
Copy link
Author

gmk57 commented Jan 29, 2021

@StelianMorariu What specific error do you get?

@StelianMorariu
Copy link

StelianMorariu commented Feb 1, 2021

@gmk57 I get an UnusedRessource for the xml layout.

I got rid of the error by using:
private val binding: ActivityBinding by viewBinding { ActivityBinding.inflate(it) }

@gmk57
Copy link
Author

gmk57 commented Feb 2, 2021

@StelianMorariu AFAIK, "unused resource" in Lint should be a warning, not an error. And it should have nothing to do with View Binding. If your code did not compile, maybe the issue was elsewhere. If you post it somewhere, I'll try to look into it.

But generally { ActivityBinding.inflate(it) } is fine too, it is equivalent of (ActivityBinding::inflate).

@StelianMorariu
Copy link

@gmk57 thanks for replying!

It was not a compilation error, it was just the Android Lint task that failed because I treat lint warnings as errors in the codebase.

I have to use com.android.tools.build:gradle:4.0.0 so I think this is likely to be the issue and not the code itself.

Anyway, thanks for posting this - it's really useful.

@gmk57
Copy link
Author

gmk57 commented Feb 22, 2021

I've updated the example for DialogFragment (the one with full-blown View) using new constructor from Fragment 1.3.0 and a regular Fragment's viewBinding delegate. This allows to safely use such a DialogFragment as an embeddable fragment. AFAIK, other use cases are not affected.

@KevinAngga
Copy link

helo @gmk57 can this be used on view data binding? i tried to used this on view data binding but got IllegalStateException

@gmk57
Copy link
Author

gmk57 commented Jul 4, 2021

@KevinAngga, I personally don't use Data Binding, but I just replaced viewBinding true with dataBinding true and added <layout> tags in my sample project, and it seems to work, because ViewDataBinding implements ViewBinding. Can you share your code and/or stack trace?

@KevinAngga
Copy link

lol so sorry @gmk57 i made some silly error by myself xD. yea nothing wrong with this one, its can be use to viewdatabinding too

@KevinAngga
Copy link

helo @gmk57 so i already use this code and roll it to production. but im getting some intermitten crash on crashlytic saying {someFragmetnName} did not return a View from onCreateView() or this was called before onCreateView(). but i cant repoduce why this happend :v did you got same problem with this one? all i know is this are illegalStateException are from requireView() code.

and i already check all my code was always call binding on onViewCreated method

@gmk57
Copy link
Author

gmk57 commented Aug 16, 2021

@KevinAngga, I've only seen this error when accessing ViewBinding outside the view lifecycle (onViewCreated-onDestroyView). This may happen if you register some callbacks/listeners/observers (e.g. in onViewCreated), and for some reason they get triggered when view does not exist. Coroutines, when incorrectly used, may cause this too. Can you share your code and/or stack trace?

@KevinAngga
Copy link

@gmk57 i cant share the code / stact trace tho cause this one are company code. but when i tried this code and accessing some binding on destroyView it wont throw that same error ._. dunno why this happen on our user device lol, any way thank for the feedback :)

@hqfranca
Copy link

@gmk57 i have tried to implement this approach in custom views but dont work, you could show a example of this implementation in custom views, please.

@gmk57
Copy link
Author

gmk57 commented Dec 12, 2021

@hqfranca Since a view has a same lifecycle as its child views, you don't really need a delegate. Simple property works fine:

class SomeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
    private val binding = SomeViewBinding.inflate(LayoutInflater.from(context), this, true)
}

My ViewGroup.viewBinding helper does not fit well here, because it passes false as a last parameter (as it should for RecyclerView.ViewHolder). You can create a second helper, but I'm not sure if it's really worth the effort: it would only save you 27 chars. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment