Android jetpack LiveData(一)使用篇

@TOC(Android jetpack LiveData (一) 使用篇)

引言

上一期我们学习了Lifecycle的相关使用和原理(有需要可以移步Lifecycle使用篇Lifecycle原理篇),本期我们将继续学习jetpack的相关架构组件LiveData的使用。

LiveData概述

LiveData是一种可观察的数据存储器类。但与常规的观察类不同,它还具有生命周期感知能力。遵循其他组件(Activity\fragment\service)的生命周期。也就是说,LiveData 只会将更新通知给活跃(生命周期处于 STARTEDRESUMED 状态 )的观察者。

它是基于观察者的消息订阅/分发组件,具有宿主(Activity/Fragment)生命周期感知能力。

LiveData优势

  • 无需自己手动更新UI界面 (由数据驱动更新)
    LiveData 遵循观察者模式。当底层数据发生变化时,LiveData 会通知 Observer 对象。您可以整合代码以在这些 Observer 对象中更新界面。这样一来,您无需在每次应用数据发生变化时更新界面,因为观察者会替您完成更新。
  • 不会有内存泄漏
    观察者会绑定到 Lifecycle 对象,并在其关联的生命周期遭到销毁后进行自我清理。
  • 不会因为Activity停止而导致崩溃
    如果观察者的生命周期处于非活跃状态(如返回堆栈中的 activity),它便不会接收任何 LiveData 事件。
  • 无需手动处理生命周期
    界面组件只是观察相关数据,不会停止或恢复观察。LiveData 将自动管理所有这些操作,因为它在观察时可以感知相关的生命周期状态变化。
  • 数据始终保持最新状态
    如果生命周期变为非活跃状态,它会在再次变为活跃状态时接收最新的数据。例如,曾经在后台的 Activity 会在返回前台后立即接收最新的数据。
  • 资源共享
    您可以使用单例模式扩展 LiveData 对象以封装系统服务,以便在应用中共享它们。LiveData 对象连接到系统服务一次,然后需要相应资源的任何观察者只需观察 LiveData 对象。

LiveData的使用

1、创建LiveData对象

LiveData 对象通常存储在 ViewModel 对象中,并可通过 getter 方法进行访问。

LiveData是一个抽象类,不能直接被使用,通常使用的是其子类MutableLiveData

kotlin 复制代码
class BlankViewModel : ViewModel() {
    
    private var progress: MutableLiveData<Int> = MutableLiveData()

    fun getProgress(): LiveData<Int> {

        return progress
    }
    
    fun setProgress(value: Int) {
        progress.value = value
    }

    override fun onCleared() {
        super.onCleared()
        progress.value = 0
    }    
}

2、设置数据变化(事件触发,或者接口回调获取的数据变化)

kotlin 复制代码
progressLD.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(
                p0: SeekBar?,
                p1: Int,
                p2: Boolean
            ) {
                // setValue在主线程中。
                progressbarLiveData.value = p1
                // postValue在子线程
                thread { 
                    progressbarLiveData.postValue(p1)
                }
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
            }
        })

3、观察liveData对象的变化,更新UI(Fragment、Activity中)

使用LifecycleOwner的observe() 方法将观察者对象附加到LiveData对象

kotlin 复制代码
 viewModel = ViewModelProvider(this).get(BlankViewModel::class.java)
 progressbarLiveData = viewModel.getProgress() as MutableLiveData<Int>
 progressbarLiveData.observe(viewLifecycleOwner, Observer<Int> {
            progressLD.progress = it
            progressLDTV.text = "当前进度为:$it"
        })

4、xml

kotlin 复制代码
	<FrameLayout
        android:id="@+id/fl_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_constraintTop_toBottomOf="parent"
        android:background="#72EF89"
        >
        <SeekBar
            android:id="@+id/progressbar_ld"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:layout_marginHorizontal="30dp"
            app:layout_constraintTop_toTopOf="parent"
            android:progress="20"
            />
        <TextView
            android:id="@+id/progressbar_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:layout_gravity="center"
            app:layout_constraintTop_toBottomOf="@id/progressbar_ld"
            android:layout_marginTop="20dp"
            />
    </FrameLayout>

这样当手动移动seekbar的进度条时,下面的文字会跟着更新UI,显示目前的进度。

数据黏性

先发布后订阅,但是可以接收到原来的数据。

发送数据,然后跳转页面

在第二个页面接收

正常思维逻辑

先订阅,再触发。可以接收数据

数据黏性思维

先触发,再订阅,仍然可以接收到订阅之前的旧数据。

相关推荐
hunterandroid33 分钟前
Android 线上卡顿治理:从布局层级到主线程负载的全链路排查
android
恋猫de小郭1 小时前
Dart Skills CLI 1.0 :AI 时代的 Dart 交付支持
android·前端·flutter
2501_916007471 小时前
使用Apple Dashboards显示和自定iOS应用性能指标与数据可视化指南
android·ios·小程序·https·uni-app·iphone·webview
蜡台2 小时前
# 已解决|MySQL 8\.4\.11 密码正确仍报错1045 \(28000\) Access denied 终极修复方案
android·mysql·adb
JMchen1232 小时前
2026年六款主流AI编程工具深度实测:Cursor、Copilot、Claude Code等对比与选型思考
android·kotlin·copilot·ai编程·开发工具·cursor·claude code
mmsx3 小时前
osmdroid 地图实战 05|让地图动起来:三个实时能力 + 六条工程排雷清单
android
码农coding3 小时前
android12 SystemUI组件之StatusBar启动
android
hai_android3 小时前
Kotlin 协程上下文:从 plus 到 CombinedContext,彻底搞懂左偏结构
android·kotlin
淡淡的香烟3 小时前
AndroidKMP之网络请求
android·网络
basketball6163 小时前
AI Infra 推理部署技术总结:2. vLLM 内核——PagedAttention 与调度器原理
android·人工智能·vllm·ai infra