记录LiveData使用时多次进入会收到旧数据的问题

使用SingleLiveEvent

/*
 *  Copyright 2017 Google Inc.
 *
 *  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 com.dhc.jmpos.bus.event;
 
import android.util.Log;
 
import java.util.concurrent.atomic.AtomicBoolean;
 
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
 
/**
 * A lifecycle-aware observable that sends only new updates after subscription, used for events like
 * navigation and Snackbar messages.
 * <p>
 * This avoids a common problem with events: on configuration change (like rotation) an update
 * can be emitted if the observer is active. This LiveData only calls the observable if there's an
 * explicit call to setValue() or call().
 * <p>
 * Note that only one observer is going to be notified of changes.
 */
public class SingleLiveEvent<T> extends MutableLiveData<T> {
 
    private static final String TAG = "SingleLiveEvent";
 
    private final AtomicBoolean mPending = new AtomicBoolean(false);
 
    @MainThread
    public void observe(LifecycleOwner owner, final Observer<? super T> observer) {
 
        if (hasActiveObservers()) {
            Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
        }
 
        // Observe the internal MutableLiveData
        super.observe(owner, new Observer<T>() {
            @Override
            public void onChanged(@Nullable T t) {
                if (mPending.compareAndSet(true, false)) {
                    observer.onChanged(t);
                }
            }
        });
    }
 
    @MainThread
    public void setValue(@Nullable T t) {
        mPending.set(true);
        super.setValue(t);
    }
 
    /**
     * Used for cases where T is Void, to make calls cleaner.
     */
    @MainThread
    public void call() {
        setValue(null);
    }
}

引用:LiveData 数据倒灌:别问,问就是不可预期_livedata数据倒灌_KunMinX的博客-CSDN博客
Android开发 SingleLiveEvent解决LiveData或者MutableLiveData多次回调的问题_singlelivdate_Clark___的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kotlin LiveData 是用于 Android 开发的一个特性,它是一个发布-订阅模式的数据源。LiveData 允许你创建可以被观察(即订阅)的数据对象,从而让你的组件知道数据发生变化。这使得数据更新更加透明,并且可以轻松地将数据传递给依赖它的组件。 使用 Kotlin LiveData 的基本步骤如下: 1. 创建一个 LiveData 对象:LiveData 对象是你想要发布的数据的公共视图。你可以将任何可变的、可观察的数据(如变量、属性或对象)封装在 LiveData 对象中。 ```kotlin val liveData = MutableLiveData<Int>() ``` 2. 将数据设置为 LiveData 对象:你可以通过调用 LiveData 对象的 set 方法将数据发布出去。 ```kotlin liveData.value = 42 ``` 3. 将 LiveData 对象传递给依赖它的组件:你可以将 LiveData 对象传递给任何需要它的组件,如 ViewModel 或 Activity/Fragment。这样,当 LiveData 中的数据发生变化,所有订阅该数据的组件都收到通知。 4. 使用观察者更新 UI:一旦你订阅了 LiveData 对象,你就可以在 UI 中使用观察者来更新 UI。当数据发生变化,UI 将自动更新。 LiveData 的优点包括: * 它提供了一种简单的方法来处理数据更新,使得组件之间的通信更加清晰和一致。 * 它允许你将数据传递给任何需要它的组件,而无需手动复制或传递数据。 * 它支持多线程操作,可以在异步操作中安全地更新 LiveData 对象。 需要注意的是,LiveData 是用于在后台线程中维护数据的,并且不能被外部观察者修改。因此,如果你需要修改数据并通知外部观察者,你可能需要使用其他方法,如使用 ViewModel 或其他观察者模式实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值