SingleLiveData

SingleLiveData

Example

การใช้งานเหมือนกับ MutableLiveData ทุกประการ แค่ตอนประกาศ Referrence type ให้ประกาศเป็น SingleLiveData แทน และการ observer ก็ทำเหมือนเดิมเช่นกัน ดังตัวอย่าง

การประกาศตัวแปร SingleLiveData

val loading = SingleLiveData<Boolean>()

การอัพเดทค่าตัวแปร SingleLiveData

loading.value = false

การ observer ตัวแปร SingleLiveData

loading.observe(this, Observer { isShow ->
    if(isShow) showLoading() else hideLoading()
})