I have seen DataBindingUtil used with all three methods, and it is not clear from the documentation (https://developer.android.com/reference/android/databinding/DataBindingUtil) what the difference is between the three.
I have seen DataBindingUtil used with all three methods, and it is not clear from the documentation (https://developer.android.com/reference/android/databinding/DataBindingUtil) what the difference is between the three.
bind takes an already inflated view hierarchy and returns a ViewDataBinding for it.
inflate takes a layout resource ID, inflates a view hierarchy from it and returns a ViewDataBinding for it. It's essentially equal to
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.some_layout, ...)
val binding = DataBindingUtil.bind<SomeLayoutBinding>(view)
setContentView takes a layout resource ID, inflates a view hierarchy from it, sets it as an activity content and returns a ViewDataBinding for the inflated view hierarchy. It's essentially equal to
setContentView(R.layout.some_layout)
val view = findViewById<View>(android.R.id.content)
val binding = DataBindingUtil.bind<SomeLayoutBinding>(view)
Generaly setContentView () will be displayed in the activity.
but fragments have a lifecycle method called onCreateView which returns a view. The most common way to do this is to inflate a view in XML and return it(as may you see in fragment's java code). In this case you need to inflate it yourself. Fragments don't have a setContentView method. so inflate use for fragments.
and binding just bind a view to a layout.