The activity base class:

(java)

package com.demo.demo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.viewbinding.ViewBinding;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

public abstract class BaseActivity<T extends ViewBinding> extends FragmentActivity {
    protected Context mContext;
    protected T vb;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.makeStatusBarTransparent(this);
        mContext = this;
        PhoneDataUtil.mobileData();
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class cls = (Class) type.getActualTypeArguments()[0];
        try {
            Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
            vb = (T) inflate.invoke(null, getLayoutInflater());
            setContentView(vb.getRoot());
        } catch (Exception e) {
            Log.e("printStackTrace", "onCreate: ", e);
            e.printStackTrace();
        }
        initView();
    }
   protected abstract void initView();   @Override
   protected void onDestroy() {    
        super.onDestroy();    
        vb = null;
   }}
Copy the code

(kotlin)

package com.demo.kotlin import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.MotionEvent import android.view.inputmethod.InputMethodManager import androidx.appcompat.app.AppCompatActivity import androidx.viewbinding.ViewBinding import java.lang.reflect.InvocationTargetException import java.lang.reflect.ParameterizedType abstract class BaseActivity <T: ViewBinding> : AppCompatActivity() { lateinit var vb: T override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val type = javaClass.genericSuperclass as ParameterizedType val cls = type.actualTypeArguments[0] as Class<*> try { val inflate = cls.getDeclaredMethod("inflate", LayoutInflater::class.java) vb = inflate.invoke(null, layoutInflater) as T setContentView(vb.root) init() } catch (e: NoSuchMethodException) { e.printStackTrace() } catch (e: IllegalAccessException) { e.printStackTrace() } catch (e: InvocationTargetException) {e.p rintStackTrace ()}} the abstract fun init () / * * * click on the blank Spaces Hide the soft keyboard * / override fun onTouchEvent(event: MotionEvent): Boolean { if (null ! = this.currentFocus) { val mInputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager return mInputMethodManager.hideSoftInputFromWindow(this.currentFocus!! .windowToken, 0) } return super.onTouchEvent(event) } }Copy the code

Fragments base class:

(java)

package com.demo.demo; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.viewbinding.ViewBinding; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.Map; public abstract class BaseFragment<T extends ViewBinding> extends Fragment { protected T vb; protected Context mContext; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); mContext = context; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass(); Class cls = (Class) type.getActualTypeArguments()[0]; try { Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class); vb = (T) inflate.invoke(null, inflater, container, false); } catch (Exception e) { e.printStackTrace(); } return vb.getRoot(); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initView(); } protected abstract void initView(); @Override public void onDestroy() { super.onDestroy(); vb = null; }}Copy the code

(kotlin)

package com.demo.kotlin import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.viewbinding.ViewBinding import java.lang.reflect.ParameterizedType abstract class BaseFragmentVB<T : ViewBinding?> : Fragment() { protected var vb: T? = null protected var mContext: Context? = null override fun onAttach(context: Context) { super.onAttach(context) mContext = context } override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? { val type = javaClass.genericSuperclass as ParameterizedType val cls = type.actualTypeArguments[0] as Class<*> try { val inflate = cls.getDeclaredMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.javaPrimitiveType ) vb = inflate.invoke(null, inflater, container, false) as T } catch (e: Exception) { e.printStackTrace() } return vb!! .root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) init() } protected abstract fun init() override fun onDestroy() { super.onDestroy() vb = null } }Copy the code