Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data between activities using ViewModel

I used the same process for sending data between fragments and it works but now I'm not getting data in Receiver Activity. Even the Log message Tag is not showing as I click on submit button. I checked in Sender Activity Log message and it is showing data but can't get those data in Receiver Activity.

Please help me to get data. Thank you!!

Getting data in Log message of Sending Activity

Receiver Activity, Not even showing Tag of Log Message

ViewModel Class:

public class ViewModelClass extends ViewModel {

private final MutableLiveData message = new MutableLiveData();

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

public MutableLiveData getMessage() {
    return message;
   }
}

Sender Activity:

    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);
}

Receiver Activity:

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 +"");
            }
        }
    });
}
like image 527
Ayush Katuwal Avatar asked Mar 08 '19 04:03

Ayush Katuwal


2 Answers

ViewModels are not shared across Activities - since you pass a different object to ViewModelProviders.of(), you'll get different ViewModel instances.

This was specifically called out in the Single Activity: Why, When, and How talk as a reason to prefer a single Activity architecture in your app.

like image 182
ianhanniballake Avatar answered Sep 21 '22 21:09

ianhanniballake


Yes Indeed, ViewModel are not shared across activities,So either you create different viewmodel for different activies or you could use different fragment with same viewmodel. Because in fragment you can achieved using SharedViewModel

like image 39
codingwithtashi Avatar answered Sep 19 '22 21:09

codingwithtashi