-1

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.

  • 1
    You 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 Answers1

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