Skip to content

Instantly share code, notes, and snippets.

public class ExampleFragment extends Fragment {
private FragmentExampleBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
return ViewBindingDelegate.inflate(
FragmentExampleBinding::inflate, b -> binding = b,
inflater, container, getViewLifecycleOwner());
killvetrov /
Last active May 3, 2021
Android View Binding: base class to reduce boilerplate in Java
public abstract class BaseViewBindingFragment extends Fragment {
private Field bindingField;
private Method inflate;
try {
for (Field declaredField : this.getClass().getDeclaredFields()) {
if (ViewBinding.class.isAssignableFrom(declaredField.getType())) {
bindingField = declaredField;