LiveData去掉粘性事件

1、去掉LiveData粘性事件主要是利用反射让mLastVersion = mVersion

package com.htf.jetpack;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by htf on 2021/6/16
 * Describe:
 */
public class LiveDataBus {
    private Map> bus;

    private static LiveDataBus liveDataBus = new LiveDataBus();
    private LiveDataBus() {
        bus = new HashMap<>();
    }

    public static LiveDataBus getInstance() {
        return liveDataBus;
    }

    public synchronized  BusMutableLiveData with(String key, Class type) {
        if (!bus.containsKey(key)) {
            bus.put(key, new BusMutableLiveData());
        }
        return (BusMutableLiveData) bus.get(key);
    }
    public static class BusMutableLiveData extends MutableLiveData {

        @Override
        public void observe(@NonNull LifecycleOwner owner, @NonNull Observer observer) {
            super.observe(owner, observer);
            hook(observer);
        }

        private void hook(Observer observer) {
            try {

                Class liveDataClass = LiveData.class;
                Field mObserversField = liveDataClass.getDeclaredField("mObservers");
                mObserversField.setAccessible(true);
                Object mObserversObject = mObserversField.get(this);
                Class mObserversObjectClass = mObserversObject.getClass();
                //获取到mObservers对象的get方法
                Method getMethod = mObserversObjectClass.getDeclaredMethod("get", Object.class);
                getMethod.setAccessible(true);
                //执行get方法
                Object invokeEntry = getMethod.invoke(mObserversObject, observer);
                Object observerWraper = null;
                if (invokeEntry != null && invokeEntry instanceof Map.Entry) {
                    observerWraper = ((Map.Entry) invokeEntry).getValue();
                }
                if (observerWraper == null) {
                    throw new NullPointerException("observerWraper 为空");
                }
                Class superclass = observerWraper.getClass().getSuperclass();
                Field mLastVersion = superclass.getDeclaredField("mLastVersion");
                mLastVersion.setAccessible(true);

                Field mVersion = liveDataClass.getDeclaredField("mVersion");
                mVersion.setAccessible(true);

                Object mVersionValue = mVersion.get(this);
                mLastVersion.set(observerWraper, mVersionValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 
 

2、使用

//绑定
LiveDataBus.getInstance().with("data", String.class).observe(this, new Observer() {
            @Override
            public void onChanged(String s) {
                Log.e("biubiubiu", "s == " + s);
            }
        });

发送

LiveDataBus.getInstance().with("data", String.class).setValue("哈哈哈" + (i++));

你可能感兴趣的:(LiveData去掉粘性事件)