this is my code inside onCreateView:
val binding = DataBindingUtil.inflate<SplashFragmentBinding>(
inflater,R.layout.splash_fragment,container,false)
My question is, why it is requred to add the type <SplashFragmentBinding>
and also send the layoutId R.layout.splash_fragment
as they practically the same. If my type is <SplashFragmentBinding>
there is no other possibility for other layoutId. Its redundancy I think.
As seen inside googles code:
public static <T extends ViewDataBinding> T inflate(@NonNull LayoutInflater inflater,
int layoutId, @Nullable ViewGroup parent, boolean attachToParent) {
return inflate(inflater, layoutId, parent, attachToParent, sDefaultComponent);
}
why it is requred to add the type and also send the layoutId R.layout.splash_fragment as they practically the same. If my type is there is no other possibility for other layoutId. Its redundancy I think.
You're right, both logically should be the same; but the reason you can provide both; that there are odd cases that the compiler can't determine the type; as In documentation:
Sometimes the binding type cannot be known in advance. In such cases, the binding can be created using the DataBindingUtil class
So, in those cases, the type has to be defined. One of which cases when you don't have a direct access to the layoutInflater
such as in ArrayAdapter
getView()
for instance; in such case they left that DataBindingUtil
method version.
But AFAIK whenever the layoutInflater
is available, you can use the other inflate()
method without referencing the layout id; for instance in your snippet you can replace it with:
val binding = SplashFragmentBinding.inflate(layoutInflater)