Is it possible to observe LiveData in custom class view? in fragments im using getViewLifecycleOwner, but what to do in a customview? Thank you very much for help.
Asked
Active
Viewed 947 times
-1
-
1You can use `observeForever` in your custom view https://stackoverflow.com/questions/52335464/setting-up-livedata-observer-in-custom-view-without-lifecycleowner – Mayur Gajra May 18 '21 at 15:47
1 Answers
1
Just wrap your custom object in liveData instance.
MutableLiveData <CustomObject> mLiveObject = new MutableLiveData<CustomObject>();
Next you have to post changes to this object using post method.
mLiveObject.postValue(mCustomObject);
Lastly you have to observe it for changes.
mLiveObject .observe(getActivity(), new Observer<CustomObject>() {
@Override
public void onChanged(CustomObject customObject) {
// do something
}
});

mohammed ahmed
- 195
- 1
- 10
-
-
Doesn't really matter. You want to observe it in child fragment? – mohammed ahmed May 18 '21 at 16:07
-
I want observe this in custom class extends LinearLayout. Is it possible ? – rmthorstennow May 18 '21 at 16:09
-
I believe it would be possible but live date is not supposed to be used in this way. – mohammed ahmed May 18 '21 at 16:14
-
Please share your use-case. It might help the community to suggest a better solution – mohammed ahmed May 18 '21 at 16:14