@Test public void testAddSameObserverIn2LifecycleOwners() { Observer<String> observer = (Observer<String>) mock(Observer.class); mLiveData.observe(mOwner, observer); Throwable throwable = null; try { mLiveData.observe(mOwner2, observer); } catch (Throwable t) { throwable = t; } assertThat(throwable, instanceOf(IllegalArgumentException.class)); assertThat(throwable.getMessage(), is("Cannot add the same observer with different lifecycles")); }
@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();  }