Solution 1 :
At the time when you are calling
mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);
you have already set active observer in onCreate
to the instance of mutableLiveData
. After you set new instance of mutableLiveData
all the observers will be unsubscribed, since the previous instance does not exist anymore, so your observer will not receive any results.
Better way of sending data to observers would be MutableLiveData.postValue(value)
. In this case instead of creating new live data instance and unsubscribe observers you are just posting a value to the active observers.
But if you need to get license data only once after user press button, you should better call Room suspend function and get plain data to display
P.S. it’s better to add observers in onViewCreated not in onCreate
Problem :
I have retrofit then repository then view model and then view, but while clicking on button observer don’t observe in onChanged. OnClicking on button from onClick, everything is working. I’m getting API response in logcat, but in onChanged it is not called!
ManualLicenseKeyViewModel
Code:
public class ManualLicenseKeyViewModel extends ViewModel {
public MutableLiveData<String> key = new MutableLiveData<>();
private MutableLiveData<License> mutableLiveData;
private ManualLicenseRepository manualLicenseRepository;
public void init() {
if (mutableLiveData == null) {
manualLicenseRepository = ManualLicenseRepository.getInstance();
}
}
public LiveData<License> getLicenseRepository() {
return mutableLiveData;
}
private void getLicenseData(String licenseKey, String macAddress, int productId) {
mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_submit:
try {
getLicenseData(key.getValue(), "null", FIXED_PRODUCT_ID);
} catch (NullPointerException e) {
e.printStackTrace();
}
break;
}
}
}
activity – onCreate:
protected void init() {
manualLicenseKeyViewModel.init();
manualLicenseKeyViewModel.getLicenseRepository().observe(this, this);
}
@Override
public void onChanged(License license) {
showLog("Working?");
try {
showLog("license: " + license.getLicensekey());
} catch (Exception e) {
}
}
NOTE: It is working in below case if I pass in init method, but the problem is, I want to perform in user click event and take value from edit text before submit button. Also I don’t want to observe in view model itself, because I want data on activity!
public void init() {
if (mutableLiveData == null) {
manualLicenseRepository = ManualLicenseRepository.getInstance();
mutableLiveData = manualLicenseRepository.getLicenseData("QQQQQ", "null", 4);
}
}
Explanation once again:
The problem is if I 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 edit text and submit button, then only I can get value which I have to pass in repository to make API call and pass that value to API.