The problem is if I\'ll write observe statement in onCreate, it won\'t observe based on user click event. Because I want to only call repository API when user fill value in
I assume the methods you posted are from your Activity. Merely adding a method named onClick
doesn't do anything at all; you need to attach a click listener to your button. For example, something like this in your onCreate
method:
findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
getLicenseData(key.getValue(), "null", FIXED_PRODUCT_ID);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
You have LiveData defined inside the method which is wrong. LiveData is something that's constantly observed by View(Activity) and therefore it needs to be held in ViewModel/Repository in a member variable.
Also, your repository looks redundant (does not do anything). I suggest redefining LiveData as a member variable of your ViewModel, making the request inside the ViewModel and posting the value to LiveData.
If you just need to send request and receive result then you don't need to even use LiveData, just use some Listener/Callback interface instead.
First I will suggest you to Add data binding in your App
now in your Activity XML
do something like this
<!--here it is 2 way binding-->
<EditText
...
android:text="@={viewmodel.key}
... />
<!--here it is single way binding-->
<Button
...
android:onClick="@{() -> viewmodel.doneClicked()}"
... />
Now in your ViewModel
//value in key will be same as value in the EditText as it is bidirectional data binding
public MutableLiveData<String> key = new MutableLiveData<>();
//update this MutableLiveData accordingly
private MutableLiveData<Boolean> isDataAvailable = new MutableLiveData<>();
//observe this LiveData
LiveData<Boolean> isDataAvailable(){
return isDataAvailable;
}
public void doneClicked(){
//here you can get value of your "key"
//this method will call when clicking on the button
//you can call your custom method from here,
//also update data in "isDataAvailable"
}
In your Activity
viewModel.isDataAvailable().observe(this, aBoolean -> {
//here you will receive change in value of isDataAvailable
}
PS Your question is not clear at all, and your code is not helpful
Do you want to observe change in mutableLiveData
of getLicenseData(...)
inside your viewModel
?
mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);
Please also change the name of this variable to something related the data it is getting, mutableLiveData is not a good variable name
Edit 1
try this to observe changes in the data, it is recommended to observe LiveData
but you can observer MutableLiveData
as you have not provided sufficient information I m using your mutableLiveData
variable
In your Activity
try this
viewModel.mutableLiveData.observe(this, license -> {
//here you can observe changes in your mutableLiveData when It receives data from Repo
//here license is the object of data inserted into mutableLiveData from repo
});
Please explain more.
Hope this will help!