What about LiveData and basic usage, please refer to the official document: developer.android.com/topic/libra…

Simply put, LiveData is an observable data container class. It wraps the data so that the data becomes “observed” and the page becomes “observer”. When the ViewModel stores the data required by the page changes, the notification of the page is realized through LiveData, and the communication between the ViewModel and the page components is completed.

Then the following points need to be paid attention to in use:

1. Callback notification

LiveData observers will receive callbacks every time they enter an active state, such as returning from background to foreground, interface rebooting, and so on. If you only want to receive one callback, you can use SingleLiveEvent. Note that a SingleLiveEvent is limited to one observer. If more than one is added, only one is called, and which one is not guaranteed.

2. Data inversion

Observe (new Obs()) after setValue/postValue, a callback is received. Observe (new anotherObs()) if the first callback is received, that is, the old data. The solution can be found in the open source project: Unpeek-LiveData.

3. Event wrapping

As mentioned above, singleliveEvents are limited to one observer. If multiple observers are required, the solution is to use event wrapping. Define a data wrapper that internally determines whether an event is consumed and does not call back notifications once consumed. The code is as follows:

/** * Event wrappers that explicitly manage whether the event has been processed * @param <T> ViewModel data, such as:  * MutableLiveData<LiveEventWrapper<String>>() */ public class LiveEventWrapper<T> { private T content; private boolean hasBeenHandled; public LiveEventWrapper(T content) { this.content = content; } /** * Returns the content and prevents its use again. */ public T getContentIfNotHandled() { if (hasBeenHandled) { return null; } else { hasBeenHandled = true; return content; } } /** * Returns the content, even if it's already been handled. */ public T peekContent() { return content; } public boolean isHasBeenHandled() { return hasBeenHandled; }}Copy the code

or

import androidx.annotation.NonNull; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; public class CleanLiveData<T> extends LiveData<T> { private boolean hasModified = false; @Override public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer<? super T> observer) { super.observe(owner, new Observer<T>() { private boolean hasIntercept = false; @Override public void onChanged(T t) { if (! hasModified || hasIntercept) { observer.onChanged(t); } hasIntercept = true; }}); } @Override public void observeForever(@NonNull final Observer<? super T> observer) { super.observeForever(new Observer<T>() { private boolean hasIntercept = false; @Override public void onChanged(T t) { if (! hasModified || hasIntercept) { observer.onChanged(t); } hasIntercept = true; }}); } @Override public void setValue(T value) { super.setValue(value); hasModified = true; } @Override public void postValue(T value) { super.postValue(value); hasModified = true; }}Copy the code

In conclusion, the above phenomena are basically caused by the viscous characteristics of LiveData, so we must be clear about its concept and principle when using LiveData.