liveData(鲜活的数据)是用来存储数据的,当activity/fragment生命周期发生变化的时候会通知LiveData,然后LiveData在发个消息到程序员代码。程序员代码里有创建好的Observer,所以LiveData即是观察者也是被观察者
LiveData如果是先发送,后注册,那么,后注册的只会收到最后一次消息
正常流程,new LiveData对象 订阅 发送消息
粘性事件流程,new LiveData对象,发送消息,订阅
liveData的非粘性事件
package com.example.lifecycleandlifedata_20211109;
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.Map;
public class NonStickyMutableLiveData<T> extends MutableLiveData {
private boolean stickFlag=false;
@Override
public void observe( LifecycleOwner owner, Observer observer) {
super.observe(owner, observer);
if(!stickFlag) {
hook(observer);
stickFlag=true;
}
}
//在这里去改变onChange的流程
private void hook(Observer<? super T> observer) {
try {
//1.得到mLastVersion
//获取到LiveData的类中的mObservers对象
//SafeIterableMap<Observer<? super T>, ObserverWrapper> mObservers
Class<LiveData> liveDataClass = LiveData.class;
Field mObserversField = liveDataClass.getDeclaredField("mObservers");
mObserversField.setAccessible(true);
//获取到这个成员变量的对象
Object mObserversObject = mObserversField.get(this);
//得到map对应的class对象
Class<?> mObserversClass = mObserversObject.getClass();
//获取到mObservers对象的get方法 entry
Method get = mObserversClass.getDeclaredMethod("get", Object.class);
get.setAccessible(true);
//执行get方法 mObservers.get(observer)
Object invokeEntry=get.invoke(mObserversObject,observer);
//定义一个空的对象
Object observerWraper=null;
if(invokeEntry!=null && invokeEntry instanceof Map.Entry){
observerWraper=((Map.Entry)invokeEntry).getValue();//ObserverWrapper
}
if(observerWraper==null){
throw new NullPointerException("observerWraper is null");
}
//得到ObserverWrapper的类对象 编译擦除问题会引起多态冲突所以用getSuperclass
//TODO:getClass()返回对应的当前正在运行时的类所对应的对
Class<?> superclass = observerWraper.getClass().getSuperclass();//mLastVersion
Field mLastVersion = superclass.getDeclaredField("mLastVersion");
mLastVersion.setAccessible(true);
//2.得到mVersion
Field mVersion = liveDataClass.getDeclaredField("mVersion");
mVersion.setAccessible(true);
//3.把mVersion的数据填入到mLastVersion中
Object mVersionValue=mVersion.get(this);
mLastVersion.set(observerWraper,mVersionValue);
}catch (Exception e){
e.printStackTrace();
}
}
}
基于LiveData的事件总线
package com.example.lifecycledemo.databus;
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;
public class LiveDataBusX {
//存放订阅者
private Map<String, MutableLiveData<Object>> bus;
private static LiveDataBusX liveDataBus = new LiveDataBusX();
private LiveDataBusX() {
bus = new HashMap<>();
}
public static LiveDataBusX getInstance() {
return liveDataBus;
}
//注册订阅者,(存入map)
public synchronized <T> MutableLiveData<T> with(String key, Class<T> type){
if(!bus.containsKey(key)){
bus.put(key,new MutableLiveData<Object>());
}
return (MutableLiveData<T>) bus.get(key);
}
//注册订阅者,(存入map) Hook前用MutableLiveData
public synchronized <T> NonStickMutableLiveData<T> withNonStick(String key, Class<T> type){
if(!bus.containsKey(key)){
bus.put(key,new NonStickMutableLiveData<Object>());
}
return (NonStickMutableLiveData<T>) bus.get(key);
}
public static class NonStickMutableLiveData<T> extends MutableLiveData<T> {
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
super.observe(owner, observer);
hook(observer);
}
//在这里去改变onChange的流程
private void hook(Observer<? super T> observer) {
try {
//1.得到mLastVersion
//获取到LiveData的类中的mObservers对象
Class<LiveData> liveDataClass = LiveData.class;
Field mObserversField = liveDataClass.getDeclaredField("mObservers");
mObserversField.setAccessible(true);
//获取到这个成员变量的对象
Object mObserversObject = mObserversField.get(this);
//得到map对应的class对象
Class<?> mObserversClass = mObserversObject.getClass();
//获取到mObservers对象的get方法
Method get = mObserversClass.getDeclaredMethod("get", Object.class);
get.setAccessible(true);
//执行get方法
Object invokeEntry=get.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 is null");
}
//得到ObserverWrapper的类对象 编译擦除问题会引起多态冲突所以用getSuperclass
Class<?> superclass = observerWraper.getClass().getSuperclass();
Field mLastVersion = superclass.getDeclaredField("mLastVersion");
mLastVersion.setAccessible(true);
//2.得到mVersion
Field mVersion = liveDataClass.getDeclaredField("mVersion");
mVersion.setAccessible(true);
//3.把mVersion的数据填入到mLastVersion中
Object mVersionValue=mVersion.get(this);
mLastVersion.set(observerWraper,mVersionValue);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
package com.example.lifecycledemo.databus;
import androidx.lifecycle.MutableLiveData;
import java.util.HashMap;
import java.util.Map;
/**
* 这条总线用于把任何类中的数据直接传递到activity或是fragment上
*/
public class LiveDataBus {
//存放订阅者
private Map<String, MutableLiveData<Object>> bus;
private static LiveDataBus liveDataBus = new LiveDataBus();
private LiveDataBus() {
bus = new HashMap<>();
}
public static LiveDataBus getInstance() {
return liveDataBus;
}
//注册订阅者,(存入map)
public synchronized <T> MutableLiveData<T> with(String key, Class<T> type){
if(!bus.containsKey(key)){
bus.put(key,new MutableLiveData<Object>());
}
return (MutableLiveData<T>) bus.get(key);
}
}