package com.duqing.missions.base; import android.os.Bundle; import android.view.LayoutInflater; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.viewbinding.ViewBinding; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; /** * Created by Administrator on 2021/10/27 0027. */ public abstract class BaseActivity extends AppCompatActivity { protected B binding; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // get genericity "B" Class entityClass = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; try { Method method = entityClass.getMethod("inflate", LayoutInflater.class);//get method from name "inflate"; binding = (B) method.invoke(entityClass,getLayoutInflater());//execute method to create a objct of viewbind; } catch (Exception e) { e.printStackTrace(); } setContentView(binding.getRoot()); } }