LiveData+ViewModel用法2:多页面监听同一个状态

1.创建ViewModel

public class SingletonViewModel extends ViewModel {
    private SingletonLiveData content;
    public SingletonLiveData getContent() {
        if (content == null) {
            content = SingletonLiveData.getInstance();
        }
        return content;
    }

    public void setContent(SingletonLiveData content) {
        this.content = content;
    }

    /**
     * 改变
     */
    public void changeContent(String text) {
        content.setValue(text);
    }
}

2.这里监听的是SingletonLiveData,这是一个单例对象,也是实现多页面监听同一个状态的关键所在。

public class SingletonLiveData extends LiveData {

    private SingletonLiveData() {
    }

    @Override
    public void postValue(String value) {
        super.postValue(value);
    }

    @Override
    public void setValue(String value) {
        super.setValue(value);
    }

    private static SingletonLiveData sInstance;

    public static SingletonLiveData getInstance() {
        if (sInstance == null) {
            sInstance = new SingletonLiveData();
        }
        return sInstance;
    }
}

3.SingletonActivity与Singleton2Activity

SingletonActivity:

		btn = findViewById(R.id.btn);

        //创建
        final SingletonViewModel viewModel = ViewModelProviders.of(this).get(SingletonViewModel.class);

        //监听
        viewModel.getContent().observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable String s) {
                Log.e("onChanged", "页面1");
                btn.setText(s);
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //点击后,文字变成  “当前状态是页面1 ”
                viewModel.changeContent("当前状态是页面1");
                new Handler(getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(SingletonActivity.this, Singleton2Activity.class));
                    }
                }, 1000);
            }
        });
Singleton2Activity:
		btn = findViewById(R.id.btn);

        //创建
        final SingletonViewModel viewModel = ViewModelProviders.of(this).get(SingletonViewModel.class);

        //监听
        viewModel.getContent().observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable String s) {
                Log.e("onChanged", "页面2");
                btn.setText(s);
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.changeContent("当前状态是页面2");
                new Handler(getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                },1000);
            }
        });

点击两个页面进行跳转时,会发现按钮的文字始终保持一致。

下载地址:
https://github.com/zhushuhao/LiveData.git

你可能感兴趣的:(android)