6

我有一个活动,它会转到片段-A,然后转到片段-B,如下所示。

Activity -> Fragment-A -> Fragment-B

情况1

这两个片段观察相同的 LiveData 以显示如下所示的snackBar。

viewModel.responseData.observe(this, Observer {
            it.getContentIfNotHandled()?.let {
            showSnackBar(it)
}

视图模型中livedata

var responseData = MutableLiveData<Event<String>>()
responseData.value = Event("$message")

错误: 当我使用上面的代码时。它只显示snackBarat fragment-A片段-B无法获取值。

情况2

当我将代码更改为以下

viewModel.responseData.observe(this, Observer {
            showSnackBar(it.peekContent())
})

两个片段都可以获取值。

错误:

关闭片段后再次转动。它显示了snackBar,因为它的值responseData仍然存在。但是我没有发送消息。

Event来自 Google 的类参考如下:

/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package bbin.mobile.ballbet.support

import androidx.lifecycle.Observer
import timber.log.Timber

/**
 * Used as a wrapper for data that is exposed via a LiveData that represents an event.
 */
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
}

/**
 * An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
 * already been handled.
 *
 * [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
 */
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
    override fun onChanged(event: Event<T>?) {
        event?.getContentIfNotHandled()?.let {
            onEventUnhandledContent(it)
        }
    }
}

我希望片段为他们显示正确的snackBar 消息。

如何livedata在Android中以正确的方式观察多个片段?

提前致谢。

4

1 回答 1

3

您正在正确地观察 LiveData,这就是Event类的设计方式。

当你这样做

viewModel.responseData.observe(this, Observer {
            it.getContentIfNotHandled()?.let {
            showSnackBar(it)
}

getContentIfNotHandled将只返回一次内容,直到再次设置新值。如果FragmentA先消费这个FragmentB将无法消费相同的价值。这是peekContent有效的原因,因为即使事件已被消耗,它也将始终返回当前值。

如果有充分的理由需要在两个片段中显示此 Snackbar 消息,我建议您观察LiveData每个片段的不同实例。

您可以通过在LiveData<Event<String>>每次viewModel.getResponseData()调用时返回一个新的来做到这一点。

于 2019-09-24T07:45:00.097 回答