How with livedata can i send some data from a service to the main activity and (the most important), how can I know that a main activity was actually observing this data so that I m sure I didn't sent the data nowhere?
Asked
Active
Viewed 352 times
1 Answers
1
Sending data from Service to Activity using LiveData may not be the best solution. You may want to consider LocalBroadcastManager or other solutions. But to answer your question, if you can somehow get a reference to your Activity's ViewModel from your Service and call "postValue" method on the MutableLiveData the new value will be applied and if your Activity is not paused or destroyed and it is observing that LiveData on the Activity LifeCycle, it should get triggered.

Harry
- 336
- 1
- 6
-
but LocalBroadcastManager is now deprecated :( so what other solution could I use? – zeus Nov 01 '22 at 05:58
-
Please see whether the following link can help. It is using a singleton LiveData located inside the service and access it by callers. https://stackoverflow.com/questions/68995642/how-can-i-update-ui-from-service-using-livedata – Android Newbie A Nov 01 '22 at 06:19
-
Just I am not sure that whether there is listener on the LiveData to observe who "attach" to or "detach" from it. I might be totally wrong since I am a regular DAO users so please forgive me if this is not the case – Android Newbie A Nov 01 '22 at 06:20