@Test public void setValue_twoObserversOneStopped_liveDataStaysActive() { Observer<String> observer1 = mock(Observer.class); Observer<String> observer2 = mock(Observer.class); mLiveData.observe(mOwner, observer1); mLiveData.observe(mOwner2, observer2); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); mRegistry2.handleLifecycleEvent(Lifecycle.Event.ON_START); verify(mActiveObserversChanged).onCall(true); reset(mActiveObserversChanged); mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); verify(mActiveObserversChanged, never()).onCall(anyBoolean()); }
@MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) { assertMainThread("observe"); if (owner.getLifecycle().getCurrentState() == DESTROYED) { 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); }
LiveData { @MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) { assertMainThread("observe"); if (owner.getLifecycle().getCurrentState() == DESTROYED) { 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); } }
LiveData { @MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) { assertMainThread("observe"); if (owner.getLifecycle().getCurrentState() == DESTROYED) { 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); }  }
LiveData { @MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) { assertMainThread("observe"); if (owner.getLifecycle().getCurrentState() == DESTROYED) { 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); }  @MainThread void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer); @MainThread void observeForever(@NonNull Observer<? super T> observer); @MainThread void removeObserver(@NonNull final Observer<? super T> observer); @SuppressWarnings("WeakerAccess") @MainThread void removeObservers(@NonNull final LifecycleOwner owner); @Nullable T getValue(); @SuppressWarnings("WeakerAccess") boolean hasObservers(); @SuppressWarnings("WeakerAccess") boolean hasActiveObservers(); }
LiveData { @MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) { assertMainThread("observe"); if (owner.getLifecycle().getCurrentState() == DESTROYED) { 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); }  @MainThread void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer); @MainThread void observeForever(@NonNull Observer<? super T> observer); @MainThread void removeObserver(@NonNull final Observer<? super T> observer); @SuppressWarnings("WeakerAccess") @MainThread void removeObservers(@NonNull final LifecycleOwner owner); @Nullable T getValue(); @SuppressWarnings("WeakerAccess") boolean hasObservers(); @SuppressWarnings("WeakerAccess") boolean hasActiveObservers();  }