Skip to content

Instantly share code, notes, and snippets.

@joseprl89
Created May 27, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseprl89/b07f68a4c999692c4901a425af9c13ee to your computer and use it in GitHub Desktop.
Save joseprl89/b07f68a4c999692c4901a425af9c13ee to your computer and use it in GitHub Desktop.
How LiveData observes a value for Internals of Android Architecture Components Part II- LiveData
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
// ignore
return;
}
LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
if (existing != null && !existing.isAttachedTo(owner)) {
throw new IllegalArgumentException("Cannot add the same observer"
+ " with different lifecycles");
}
if (existing != null) {
return;
}
owner.getLifecycle().addObserver(wrapper);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment