Fre*_*ddy 9 android kotlin-flow kotlin-sharedflow
当导航回片段时,如何避免再次执行collect{}代码。
视图模型类
private val _commitResult = MutableStateFlow<Map<String, Any>>(mapOf())
val commitResult: StateFlow<Map<String, Any>> = _commitResult
Fragment code like this:
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED){
viewModel.commitResult.collect { data ->
Logger.i("commitResult $data")
//navigate to another fragment
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我首先更改 viewModel 中的 _commitResult 值时,跳转到另一个片段效果很好。不幸的是,当我回到片段时。collect{ // navigate to another fragment}将再次执行。
我知道什么时候回到片段。onCreateView 再次执行,viewModel 会发出之前的数据存储,因此执行collect { // navigate to another fragment}。我怎样才能避免这种情况?
与 LiveData 相同,我使用 Event 来修复 LiveData 的问题。
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
}
Run Code Online (Sandbox Code Playgroud)
我该如何用 stateflow 处理这个问题?实际上我不喜欢 Event<.> 来处理这个问题,我是否以错误的方式使用状态流?我该如何解决这个问题?如果有人可以提供帮助,请提前致谢。
StateFlow 保持其状态,所以我建议:
A) 使用SharedFlow. https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/
B) 使用处理事件消除的模式
class Vm: ViewModel() {
private val mEvent = MutableStateFlow<MyResult?>(null)
val event = mEvent.asStateFlow()
fun dismissEvent() {
mEvent.value = null
}
}
class Frag: Fragment() {
override fun onViewCreated() {
vm.event.collect {
navigate()
vm.dismissEvent()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1400 次 |
| 最近记录: |