diff --git a/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/EmptySingleLiveEvent.kt b/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/EmptySingleLiveEvent.kt new file mode 100644 index 0000000..cd16acf --- /dev/null +++ b/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/EmptySingleLiveEvent.kt @@ -0,0 +1,28 @@ +package ru.touchin.lifecycle.livedata + +import androidx.annotation.MainThread + +/** + * A lifecycle-aware observable that sends only new updates after subscription, used for events like + * navigation and Snackbar messages. + * + * + * This avoids a common problem with events: on configuration change (like rotation) an update + * can be emitted if the observer is active. This LiveData only calls the observable if there's an + * explicit call to setValue() or call(). + * + * + * Note that only one observer is going to be notified of changes. + * + * This version of SingleLiveEvent supports empty events + */ +class EmptySingleLiveEvent : SingleLiveEvent() { + @MainThread + fun call() { + value = null + } + + fun postCall() { + super.postValue(null) + } +} diff --git a/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/SingleLiveEvent.kt b/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/SingleLiveEvent.kt index 073e310..fc13075 100644 --- a/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/SingleLiveEvent.kt +++ b/lifecycle/src/main/java/ru/touchin/lifecycle/livedata/SingleLiveEvent.kt @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer import java.util.concurrent.atomic.AtomicBoolean -class SingleLiveEvent : MutableLiveData() { +open class SingleLiveEvent : MutableLiveData() { private val pending = AtomicBoolean(false)