Skip to content

Instantly share code, notes, and snippets.

public class ExampleFragment extends Fragment {
private FragmentExampleBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
return ViewBindingDelegate.inflate(
FragmentExampleBinding::inflate, b -> binding = b,
inflater, container, getViewLifecycleOwner());
@killvetrov
killvetrov / BaseViewBindingFragment.java
Last active November 15, 2022 12:58
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;