Resolve #57 Add support of SingleLiveEvent<Void>

This commit is contained in:
Daniil Shevtsov 2019-08-20 15:42:38 +03:00
parent be6d07edd1
commit ffab47c3b9
2 changed files with 29 additions and 1 deletions

View File

@ -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<Void?>() {
@MainThread
fun call() {
value = null
}
fun postCall() {
super.postValue(null)
}
}

View File

@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean
class SingleLiveEvent<T> : MutableLiveData<T>() {
open class SingleLiveEvent<T> : MutableLiveData<T>() {
private val pending = AtomicBoolean(false)