Fixed
Status Update
Comments
yb...@google.com <yb...@google.com>
se...@google.com <se...@google.com> #2
since these are in public API (:/) we need to do this in 1.2
[Deleted User] <[Deleted User]> #3
since it is already marked as deprecated, we can probably do it by now.
Description
Version used: 1.0.0 Alpha9-1
Devices/Android versions reproduced on: Moto G3, Android 6.0.1
If you add a LiveData as a source to a MediatorLiveData and then remove it again inside the observer, the source LiveData gets back into active state, even though there shouldn't be any more observers registered to it:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// source LiveData:
MutableLiveData<String> stringData = new MutableLiveData<String>() {
@Override
protected void onActive() {
super.onActive();
System.out.println("stringData onActive()");
}
@Override
protected void onInactive() {
super.onInactive();
System.out.println("stringData onInactive");
}
};
// receiving MediatorLiveData:
MediatorLiveData<Integer> intData = new MediatorLiveData<>();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// In LifecycleOwner:
intData.addSource(stringData, s -> {
System.out.println("stringData emitted " + s);
intData.removeSource(stringData);
});
// note the order here, value of the source LiveData is set before
// observer gets registered:
stringData.setValue("blabla");
intData.observe(this, integer -> {});
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Output:
System.out: stringData onActive()
System.out: stringData emitted blabla
System.out: stringData onInactive
System.out: stringData onActive()
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
As you see, the source LiveData switches back into active state, even though it doesn't have any active observers. This only happens if setValue() is called before registering the LifecycleOwner/Observer.