/**
* LiveDataBus 数据交互帮助类
*
*
* Created by
* Change
*/
public class LiveDataBus {
public static final Map> bus = new HashMap<>();
public static synchronized MutableLiveData toObservable(Class type){
String key = type.getSimpleName();
if (!bus.containsKey(key)){
bus.put(key, new BusMutableLiveData<>());
}
return (MutableLiveData) bus.get(key);
}
public static void post(Object type){
String key = type.getClass().getSimpleName();
if (!bus.containsKey(key)){
bus.put(key, new BusMutableLiveData<>());
}
Objects.requireNonNull(bus.get(key)).postValue(type);
}
private static class ObserverWrapper implements Observer {
private Observer observer;
public ObserverWrapper(Observer observer) {
this.observer = observer;
}
@Override
public void onChanged(@Nullable T t) {
if (observer != null) {
if (isCallOnObserve()) {
return;
}
observer.onChanged(t);
}
}
private boolean isCallOnObserve() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace != null && stackTrace.length > 0) {
for (StackTraceElement element : stackTrace) {
if ("androidx.lifecycle.LiveData".equals(element.getClassName()) &&
"observeForever".equals(element.getMethodName())) {
return true;
}
}
}
return false;
}
}
public static class BusMutableLiveData extends MutableLiveData {
private Map observerMap = new HashMap<>();
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer super T> observer) {
super.observe(owner, observer);
try {
hook(observer);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void observeForever(@NonNull Observer super T> observer) {
if (!observerMap.containsKey(observer)) {
observerMap.put(observer, new ObserverWrapper(observer));
}
super.observeForever(Objects.requireNonNull(observerMap.get(observer)));
}
@Override
public void removeObserver(@NonNull Observer super T> observer) {
Observer realObserver = null;
if (observerMap.containsKey(observer)) {
realObserver = observerMap.remove(observer);
} else {
realObserver = observer;
}
super.removeObserver(realObserver);
}
private void hook(@NonNull Observer super T> observer) throws Exception {
//get wrapper's version
Class classLiveData = LiveData.class;
Field fieldObservers = classLiveData.getDeclaredField("mObservers");
fieldObservers.setAccessible(true);
Object objectObservers = fieldObservers.get(this);
Class> classObservers = objectObservers.getClass();
Method methodGet = classObservers.getDeclaredMethod("get", Object.class);
methodGet.setAccessible(true);
Object objectWrapperEntry = methodGet.invoke(objectObservers, observer);
Object objectWrapper = null;
if (objectWrapperEntry instanceof Map.Entry) {
objectWrapper = ((Map.Entry) objectWrapperEntry).getValue();
}
if (objectWrapper == null) {
throw new NullPointerException("Wrapper can not be bull!");
}
Class> classObserverWrapper = objectWrapper.getClass().getSuperclass();
Field fieldLastVersion = classObserverWrapper.getDeclaredField("mLastVersion");
fieldLastVersion.setAccessible(true);
//get livedata's version
Field fieldVersion = classLiveData.getDeclaredField("mVersion");
fieldVersion.setAccessible(true);
Object objectVersion = fieldVersion.get(this);
//set wrapper's version
fieldLastVersion.set(objectWrapper, objectVersion);
}
}
}
注册订阅:
LiveDataBus.toObservable(TokenEvent.class).observeForever(result -> {// 做退出登录的操作
exit();
LoginActivity.startActivity(this);
});
发送消息:
LiveDataBus.post(new TokenEvent());
实践是检验真理的唯一标准----古人说的话没错。最后,希望能帮助到需要的人。有问题请指正,谢谢!下面是查阅的相关资料。有需要的可以去看看。原理什么的可以去了解一下下。我就不去复述照搬了。大家互相学习吧!共勉