/**
* 这条总线的用于把任何类中的数据直接传递到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, boolean sticky){
if (bus.containsKey(key)){
if (sticky){
bus.put(key, new MutableLiveData<Object>());
}else {
bus.put(key, new NonStickyMutableLiveData<Object>());
}
}
return (MutableLiveData<T>) bus.get(key);
}
}
使用liveDataBus 管理liveData
于 2022-03-27 21:03:59 首次发布