美文网首页
JetPack_LiveData的基本使用

JetPack_LiveData的基本使用

作者: 遇见开始遇见 | 来源:发表于2020-08-26 15:55 被阅读0次

1,封装liveData工具类

```

 setContentView(R.layout.activity_test_liva_data_bus);

        LiveDataBus.getInstance().with("data",String.class)

.observe(this, new Observer() {

```

public class TestLiveDataBus {

//存放订阅者

    private Map>bus;

    private TestLiveDataBus(){

bus =new HashMap<>();

 }

private static TestLiveDataBusliveDataBus =new TestLiveDataBus();

    public static TestLiveDataBusgetInstance(){

return liveDataBus;

    }

//注册订阅者

    public MutableLiveDatawith(String key,Class type){

if (!bus.containsKey(key)){

bus.put(key,new MutableLiveData());

        }

return (MutableLiveData)bus.get(key);

    }

}

2.在A   activity 中发送消息 

LiveDataBus.getInstance().with("data",String.class).setValue("danny");

3.在B  activity 中使使用获取消息

public class TestLivaDataBusActivityextends AppCompatActivity {

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_liva_data_bus);

        LiveDataBus.getInstance().with("data",String.class)

.observe(this, new Observer() {

@Override

            public void onChanged(String s) {

Toast.makeText(TestLivaDataBusActivity.this,s,Toast.LENGTH_SHORT).show();

            }

});

    }

}

4.就是这么简单 学会了吗!

相关文章

  • JetPack_LiveData的基本使用

    1,封装liveData工具类 ``` setContentView(R.layout.activity_test...

  • 基本的使用

    存cookie 取cookie 存session 取session

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

  • SQL语句基本使用

    SQL语句基本使用——增删改查 SQL语句基本使用——WHERE子句 SQL语句基本使用——AND和OR的使用 S...

  • 6-xpath和css select基本使用

    Xpath基本使用 css select基本使用

  • NSInvocation的基本使用

    前提: 在 iOS中可以直接调用某个对象的消息方式有两种:一种是performSelector:withObjec...

网友评论

      本文标题:JetPack_LiveData的基本使用

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