Created
September 20, 2017 13:00
-
-
Save PierceZ/9d957f8f658f8e2d9e265f9fc314ba36 to your computer and use it in GitHub Desktop.
Another approach where EventLiveData contains a reference to its lifecycle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EventLiveData extends LiveData<Object> { | |
private final int mSubject; | |
private final LifecycleRegistryOwner mLifecycle; | |
public EventLiveData(@LiveDataBus.Subject int subject, @NonNull LifecycleRegistryOwner lifecycle) { | |
mSubject = subject; | |
mLifecycle = lifecycle; | |
} | |
public void update(Object object) { | |
setValue(object); | |
} | |
@Override | |
public void removeObserver(Observer<Object> observer) { | |
super.removeObserver(observer); | |
if (!hasObservers()) { | |
LiveDataBus.unregister(mSubject, mLifecycle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment