diff --git a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java index 97690d596f..aeb17225ab 100644 --- a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java +++ b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java @@ -40,7 +40,9 @@ * @param the type of the RealmModel */ public class LiveRealmObject extends LiveData { - // The listener will listen until the object is deleted. + private final T object; + + // The listener will listen until the object is deleted. // An invalidated object shouldn't be set in LiveData, null is set instead. private RealmObjectChangeListener listener = new RealmObjectChangeListener() { @Override @@ -72,7 +74,7 @@ public LiveRealmObject(@NonNull T object) { if (!RealmObject.isValid(object)) { throw new IllegalArgumentException("The provided RealmObject is no longer valid, and therefore cannot be observed for changes."); } - setValue(object); + this.object = object; } // We should start observing and stop observing, depending on whether we have observers. @@ -87,7 +89,11 @@ protected void onActive() { super.onActive(); T object = getValue(); if (object != null && RealmObject.isValid(object)) { + setValue(object); RealmObject.addChangeListener(object, listener); + } else if (RealmObject.isValid(this.object)) { + setValue(this.object); + RealmObject.addChangeListener(this.object, listener); } }