相同的值会触发 LiveDate 中的事件吗?
Will the same value trigger the event in LiveDate?
代码A和图片A来自文章LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)。
作者告诉我"Trigger the event by setting a new Event as a new value",我觉得应该是"Trigger the event by setting a new Event as any value"吧?
例如,
第 1 步:用户单击主 Activity 中带有代码 userClicksOnButton("StartDetails")
的按钮,详细信息 Activity 将启动。
第 2 步:用户按下返回,回到 master activity
第三步:用户再次点击代码userClicksOnButton("StartDetails")
的masterActivity中的按钮,DetailsActivity将重新开始。
对吗?
代码A
class ListViewModel : ViewModel {
private val _navigateToDetails = MutableLiveData<Event<String>>()
val navigateToDetails : LiveData<Event<String>>
get() = _navigateToDetails
fun userClicksOnButton(itemId: String) {
_navigateToDetails.value = Event(itemId) // Trigger the event by setting a new Event as a new value
}
}
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
myViewModel.navigateToDetails.observe(this, Observer {
it.getContentIfNotHandled()?.let { // Only proceed if the event has never been handled
startActivity(DetailsActivity...)
}
})
图片A
您对数据有两种不同的约束。
LiveData
向活跃的观察者发出每次更新,但事件本身是有状态的。因此需要 new 事件作为新的 LiveData
值。 itemId
可以是任何值,但注释没有引用它。
Trigger the event by setting a new Event as [a new] value
代码A和图片A来自文章LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)。
作者告诉我"Trigger the event by setting a new Event as a new value",我觉得应该是"Trigger the event by setting a new Event as any value"吧?
例如,
第 1 步:用户单击主 Activity 中带有代码 userClicksOnButton("StartDetails")
的按钮,详细信息 Activity 将启动。
第 2 步:用户按下返回,回到 master activity
第三步:用户再次点击代码userClicksOnButton("StartDetails")
的masterActivity中的按钮,DetailsActivity将重新开始。
对吗?
代码A
class ListViewModel : ViewModel {
private val _navigateToDetails = MutableLiveData<Event<String>>()
val navigateToDetails : LiveData<Event<String>>
get() = _navigateToDetails
fun userClicksOnButton(itemId: String) {
_navigateToDetails.value = Event(itemId) // Trigger the event by setting a new Event as a new value
}
}
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
myViewModel.navigateToDetails.observe(this, Observer {
it.getContentIfNotHandled()?.let { // Only proceed if the event has never been handled
startActivity(DetailsActivity...)
}
})
图片A
您对数据有两种不同的约束。
LiveData
向活跃的观察者发出每次更新,但事件本身是有状态的。因此需要 new 事件作为新的 LiveData
值。 itemId
可以是任何值,但注释没有引用它。
Trigger the event by setting a new Event as [a new] value