How to clear LiveData stored value?

后端 未结 7 1299
暖寄归人
暖寄归人 2021-01-30 02:34

According to LiveData documentation:

The LiveData class provides the following advantages:

...

Always up to date data: If a Li

7条回答
  •  野性不改
    2021-01-30 02:45

    If you need simple solution, try this one:

    class SingleLiveData : MutableLiveData() {
    
        override fun observe(owner: LifecycleOwner, observer: Observer) {
            super.observe(owner, Observer { t ->
                if (t != null) {
                    observer.onChanged(t)
                    postValue(null)
                }
            })
        }
    }
    

    Use it like a regular MutableLiveData

提交回复
热议问题