我有一个与状态兼容的接口。
interface HasState<S> {
val state: S?
}
,在实现中,状态通过liveData公开。一切正常,如下所示:
class StateThing: HasState<State> {
override val state = State()
val liveData = MutableLiveData<State>()
// functions which update liveData
}
除了我将实时数据移至HasState
接口并在实现中覆盖它的那一刻,它都中断了。这不起作用:
interface HasState<S> {
val state: S?
val liveData: MutableLiveData<S>
}
class StateThing: HasState<State> {
override val state = State()
override val liveData = MutableLiveData<State>()
// functions which update liveData
}
HasState仍然像以前一样公开liveData,但是现在liveData.observe(lifecycleOwner)
函数从不接收Observer事件。
为什么两者之间有区别?