Is it possible to prevent LiveData
receive the last value when start observing? I am considering to use LiveData
as events.
For example events like show message, a navigation event or a dialog trigger, similar to EventBus
.
The problem related to communication between ViewModel
and fragment, Google gave us LiveData
to update the view with data, but this type of communication not suitable when we need update the view only once with single event, also we cannot hold view's reference in ViewModel
and call some methods because it will create memory leak.
I found something similar SingleLiveEvent - but it work only for 1 observer and not for multiple observers.
--- Update ----
As @EpicPandaForce said "There is no reason to use LiveData as something that it is not", probably the intent of the question was Communication between view and ViewModel in MVVM with LiveData
StateFlow and LiveData have similarities. Both are observable data holder classes, and both follow a similar pattern when used in your app architecture. The StateFlow and LiveData do behave differently: StateFlow requires an initial state to be passed into the constructor, while LiveData does not.
You should manually call removeObserver(Observer) to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active. If the observer was already added with an owner to this LiveData, LiveData throws an IllegalArgumentException .
LiveData is lifecycle-aware, following the lifecycle of entities such as activities and fragments. Use LiveData to communicate between these lifecycle owners and other objects with a different lifespan, such as ViewModel objects.
I`m using this EventWraper class from Google Samples inside MutableLiveData
/** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException("null values in Event are not allowed."); } mContent = content; } @Nullable public T getContentIfNotHandled() { if (hasBeenHandled) { return null; } else { hasBeenHandled = true; return mContent; } } public boolean hasBeenHandled() { return hasBeenHandled; } }
In ViewModel :
/** expose Save LiveData Event */ public void newSaveEvent() { saveEvent.setValue(new Event<>(true)); } private final MutableLiveData<Event<Boolean>> saveEvent = new MutableLiveData<>(); public LiveData<Event<Boolean>> onSaveEvent() { return saveEvent; }
In Activity/Fragment
mViewModel .onSaveEvent() .observe( getViewLifecycleOwner(), booleanEvent -> { if (booleanEvent != null) final Boolean shouldSave = booleanEvent.getContentIfNotHandled(); if (shouldSave != null && shouldSave) saveData(); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With