美文网首页
Jetpack架构之LiveData

Jetpack架构之LiveData

作者: NengLee | 来源:发表于2021-01-17 22:51 被阅读0次

LiveData是一个可以被观察的数据持有类,它具有感知 Activity,Fragmen或是Servers等组件的生命周期。而且LiveData和ViewMode是经常搭配在一起使用的。

特点

  • 感知生命周期

    • LiveData知晓与它绑定的上下文(Activity/Fragmen)的生命周期,并且只会给前台活动回调,不用担心View处于onDestroy赋值导致的异常等内存泄漏
  • 基于观察者模式

    • LiveData也是一个观察者模式的数据实体类,可以做到事件通信机制,并且可以通知所有观察者,可替代rxjava、EventBus
  • 数据持有容器

    • LiveData具有数据实体类(POJO)的一样特性,它可以负责暂存数据源

LiveData使用

  • Build.gradle
implementation "androidx.lifecycle:lifecycle-livedata:2.2.0"
  • 创建一个LiveData
package com.example.livedata;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MyLiveData extends ViewModel {


    //MutableLiveData是LiveData的子类,
    private MutableLiveData<String> mutableLiveData;

    //获取 MutableLiveData对象,
    public MutableLiveData<String> getMutableLiveData() {
        if (mutableLiveData == null)
            mutableLiveData = new MutableLiveData<>();

        return mutableLiveData;
    }
}
  • MainActivity
package com.example.livedata;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    private MyLiveData myLiveData;
    private String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLiveData = new MyLiveData();

        //观察者
        Observer<String> observer = s -> {
            Log.e(TAG, "onChanged: " + s);
            ((TextView) findViewById(R.id.tex_tv)).setText(s);
        };

        //注册观察者 带当前生命周期感知
        myLiveData.getMutableLiveData().observe(this, observer);
        //注册观察者,无
        //myLiveData.getMutableLiveData().observeForever(observer);
    }


    public void onButton(View view) {
        //发送一个Message事件
        myLiveData.getMutableLiveData().setValue("Hello LiveData");
    }
}

当点击But事件便会回调Observe的观察者的void onChanged(T t); 回调方法。

总线Bus管理LiveData

  • LiveDataBus 用 busMap 集合管理所有MutableLiveData ,通过key进行创建查找
package com.example.livedata.Bus;

import androidx.lifecycle.MutableLiveData;

import java.util.HashMap;
import java.util.Map;

public class LiveDataBus<T> {

    //存放观察者
    private static Map<String, MutableLiveData<Object>> busMap;

    private static LiveDataBus liveDataBus = new LiveDataBus();

    public synchronized static <T> LiveDataBus<T> getLiveDataBus() {
        if (busMap == null)
            busMap = new HashMap<>();
        return liveDataBus;
    }
    
    //注册观察者
    public synchronized <T> MutableLiveData<T> with(String ket, Class<T> tClass) {
        //查询
        if (!busMap.containsKey(ket)) {
            //创建一个MutableLiveData
            busMap.put(ket, new MutableLiveData<>());
        }
        //返回当期需要的MutableLiveData
        return (MutableLiveData<T>) busMap.get(ket);
    }
}

  • 注册/监听

        LiveDataBus.getLiveDataBus().with("keys", String.class).observe(this,s -> {
            Log.e("TAG", "onCreate  observeForever: " + s);
        });
    

相关文章

网友评论

      本文标题:Jetpack架构之LiveData

      本文链接:https://www.haomeiwen.com/subject/edazaktx.html