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 +"");
            }
        }
    });
}
Ayush Katuwal
  • 139
  • 1
  • 2
  • 11
  • see https://github.com/googlesamples/android-architecture-components/issues/29 and https://stackoverflow.com/a/49364903/2252830 for example – pskink Mar 08 '19 at 04: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 `ViewModel`s by making it static. Another solution is to share data using intents. – Jeel Vankhede Mar 08 '19 at 04: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. – Ayush Katuwal Mar 08 '19 at 05:12
  • so see [Data and file storage overview](https://developer.android.com/guide/topics/data/data-storage) – pskink Mar 08 '19 at 05: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`. – CommonsWare Mar 08 '19 at 12:25
  • I guess the solution is to use a viewmodel at the application level rather than the activity. Unfortunately smart engineers at Google only think of the activity and not the application. These kinds of issues are solve in Windows for many years. – saeed khalafinejad Jul 12 '20 at 04:05

2 Answers2

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
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

Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30