Open dglozano opened 5 years ago
@dglozano Any progress on this?
@KwabenBerko Not that I know of... just the answers to the Stackoverflow post.
@dglozano Cool. Can you kindly shed more light on how you personally implemented this though?
@KwabenBerko sure no problem, but I would probably try to do it differently if I had to do it all over again. This was just an university project.
What I did was to just keep the data read by the BoundService inside itself and expose it using LiveData. Then the fragment inside the activity connected to the service would observe the data stored in the LiveData in the service and pass it down to the fragment view model.
You can check the source code in
https://github.com/dglozano/escale-mobile
More specifically
https://github.com/dglozano/escale-mobile/blob/master/app/src/main/java/com/dglozano/escale/ui/main/home/HomeFragment.java https://github.com/dglozano/escale-mobile/blob/master/app/src/main/java/com/dglozano/escale/ui/main/MainActivity.java https://github.com/dglozano/escale-mobile/blob/master/app/src/main/java/com/dglozano/escale/ble/BF600BleService.java
Awesome. Thanks!
Just to clarify, it would be this option from my SO post:
Cheers!
Hello,
First of all, sorry for posting it here. I know it is "out of scope" and that it should go ask in StackOverflow. I have already done it here.
Some people have already tried to answer, but I feel there is a lot of confusion about where to place Android (Bound) Services in the recommended architecture. I have read over and over again your guides, medium posts and other issues here, here and here, but I cannot find information about this topic.
I am pretty sure I should place them on top of the architecture and treat them just like an Activity/Fragment, because BoundServices are part of the Android Framework, they are managed by the Android OS and they are bound to other Activities and Fragments. In that case, I don't know what's the best way to interact with LiveData, ViewModels and Activities/Fragments.
Some might think that they should be considered as a DataSource (since in my case it's getting data from a scale using Bluetooth), but I don't think this is a good idea, because of all what I've said in the previous paragraph and specially because of what it says here:
Even though the community might give thoughtful answers in StackOverflow, I would really appreciate if I can get a word from you about this matter, because I feel it will be useful for other developers as well.
Thanks! Diego.-