8

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 +"");
            }
        }
    });
}
6
  • see github.com/googlesamples/android-architecture-components/issues/… and stackoverflow.com/a/49364903/2252830 for example Commented Mar 8, 2019 at 4:56
  • Simple answer is you can't. Why is because activities can't share ViewModel instances in between, even if you do, it violates it's lifecycle retention policy. One workaround is to use ViewModelFactory to share your ViewModels by making it static. Another solution is to share data using intents. Commented Mar 8, 2019 at 4:57
  • Hope I could use Intent to pass values, but in my case I've to get values from three activities and finally submit those values in fourth activity. Intent can help me to solve my problem but as there are many fields so it can be hard to handle those data. Actually I'm also new in Programming so can you help me to use ViewModelFactory ? Please send me the effective link of ViewModelFactory to send data between activities. Commented Mar 8, 2019 at 5:12
  • so see Data and file storage overview Commented Mar 8, 2019 at 5:14
  • "but in my case I've to get values from three activities and finally submit those values in fourth activity" -- if they are that closely coupled, perhaps they should not be separate activities. "Please send me the effective link of ViewModelFactory to send data between activities" -- that is not a proper use of ViewModelFactory. Commented Mar 8, 2019 at 12:25

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you #ianhanniballake. I'm trying to use ViewModelFactory to perfom passing data between Activities.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.