android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.45k stars 8.28k forks source link

[Question] Where to place BoundService #508

Open dglozano opened 5 years ago

dglozano commented 5 years ago

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:

Avoid designating your app's entry points—such as activities, services, and broadcast receivers—as sources of data. Instead, they should only coordinate with other components to retrieve the subset of data that is relevant to that entry point. Each app component is rather short-lived, depending on the user's interaction with their device and the overall current health of the system.

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.-

KwabenBerko commented 4 years ago

@dglozano Any progress on this?

dglozano commented 4 years ago

@KwabenBerko Not that I know of... just the answers to the Stackoverflow post.

KwabenBerko commented 4 years ago

@dglozano Cool. Can you kindly shed more light on how you personally implemented this though?

dglozano commented 4 years ago

@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

KwabenBerko commented 4 years ago

Awesome. Thanks!

dglozano commented 4 years ago

Just to clarify, it would be this option from my SO post:

image

Cheers!