如何使用ViewModel在活动之间发送数据

时间:2019-03-08 04:48:40

标签: android android-viewmodel android-mvvm

我使用相同的过程在片段之间发送数据,并且可以正常工作,但是现在我没有在Receiver Activity中获取数据。当我单击提交按钮时,甚至日志消息标记也没有显示。我签入了“发件人活动日志”消息,它显示数据,但无法在“接收者活动”中获取这些数据。

请帮助我获取数据。谢谢!!

Getting data in Log message of Sending Activity

Receiver Activity, Not even showing Tag of Log Message

ViewModel类:

public class ViewModelClass extends ViewModel {

private final MutableLiveData message = new MutableLiveData();

public void setMessage(HomeModelClass data){
    message.setValue(data);
}

public MutableLiveData getMessage() {
    return message;
   }
}

发件人活动:

    public class EditHomeData extends AppCompatActivity {

    private ViewModelClass viewModelClass;

    HomeModelClass homeModelClassData = new HomeModelClass();

    @Override
    protected void onCreate(Bundle savedInsatancestate) {
    super.onCreate(savedInsatancestate);
    setContentView(R.layout.first_page);

    viewModelClass = ViewModelProviders.of(this).get(ViewModelClass.class);

    setValues();

  });

    public void setValues() {

    if (yes.isChecked()) {
        rent_value = String.valueOf(1);
    } else if (no.isChecked()) {
        rent_value = String.valueOf(0);
    }

    homeModelClassData.setWard_id(ward_id + "");
    homeModelClassData.setToleName(tole_name.getText().toString());
    homeModelClassData.setHouseAge(house_age.getText().toString());
    homeModelClassData.setRadio(rent_value);
    homeModelClassData.setTotal_tenant(editText1.getText().toString());
    homeModelClassData.setMale_tenant(editText2.getText().toString());
    homeModelClassData.setFemale_tenant(editText3.getText().toString());
    homeModelClassData.setHouse_stroyes(spi1);
    homeModelClassData.setRoof_types(spi2);
    homeModelClassData.setLatitude(lati.getText().toString());
    homeModelClassData.setLongitude(longi.getText().toString());

    viewModelClass.setMessage(homeModelClassData);
}

接收者活动:

public class EditHomeData3 extends AppCompatActivity {

Button submit, cancel;

String ward_id, houseNumber, toleName, house_age, radio, total_tenant, male_tenant, female_tenant, house_stroyes,
        roof_types, latitude, longitude, value_updateby;

@Override
protected void onCreate(Bundle savedInsatancestate) {
    super.onCreate(savedInsatancestate);
    setContentView(R.layout.third_page);

    submit = findViewById(R.id.submit_btn);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getDatafromField();
        }
    });

    private void getDatafromField() {

    final ViewModelClass model = ViewModelProviders.of(this).get(ViewModelClass.class);

    model.getMessage().observe(this, new Observer() {
        @Override
        public void onChanged(@Nullable Object o) {

            if (o instanceof HomeModelClass) {

                HomeModelClass homedata = (HomeModelClass) o;

                ward_id = homedata.getWard_id();
                houseNumber = homedata.getHouseNumber();
                toleName = homedata.getToleName();
                house_age = homedata.getHouseAge();
                radio = homedata.getRadio();
                total_tenant = homedata.getTotal_tenant();
                male_tenant = homedata.getMale_tenant();
                female_tenant = homedata.getFemale_tenant();
                house_stroyes = homedata.getHouse_stroyes();
                roof_types = homedata.getRoof_types();
                latitude = homedata.getLatitude();
                longitude = homedata.getLongitude();
                value_updateby = String.valueOf("1");

                Log.i("GetMessage", houseNumber +"");
            }
        }
    });
}

2 个答案:

答案 0 :(得分:3)

ViewModels在活动之间不共享-由于将不同的对象传递给ViewModelProviders.of(),因此您将获得不同的ViewModel实例。

Single Activity: Why, When, and How talk中明确指出了这一点,作为在您的应用中首选单一Activity架构的原因。

答案 1 :(得分:1)

确实,ViewModel 不会跨活动共享,因此您可以为不同的活动创建不同的视图模型,或者您可以使用具有相同视图模型的不同片段。因为在片段中您可以使用 SharedViewModel