BoundService + LiveData + ViewModel best practice in new Android recommended architecture











up vote
11
down vote

favorite
4












I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach.



I did a lot of research, and I couldn't find any useful guideline nor tutorial. The only hint I found about where to place the Service in my app architecture is this one, from @JoseAlcerreca Medium post




Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.




According to that, I should place my Android Services on the top of my Architecture Components hierarchy, at the same level as my Activities and Fragments. That's because Android Services are part of the Android framework, so ViewModels shouldn't know about them.



Now, I will explain briefly my scenario, but only to make the panorama clearer, not because I want an answer for this specific scenario.




  • I have an Android Application that has a MainActivity with many fragments in it, all of them tied together in a BottomNavBar.

  • I have a BluetoothService bound to myActivity and one of its fragments (because I want the Service to have the same lifecycle as the Activty but I also want to interact with it directly from my fragment).

  • The fragment interacts with the BluetoothService to get two types of information:


    • Information about the state of the Bluetooth connection. Doesn't need to be persisted.

    • Data that comes from the Bluetooth Device (it is a Scale, so weight and body composition in this case). Needs to be persisted.




Here are the 3 different architectures I can think of:



LiveData inside AndroidService
LiveData inside Android Service arch




  • The LiveData with the state of the connection and with the weight
    measurements coming from the Bluetooth Device are inside the BluetoothService.

  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The Fragment observes the LiveData about the state of the connection
    and adapts the UI accordingly (for example, enable a button if the
    state is connected).

  • The Fragment observes the LiveData of the new weight measurements. If a new weight measurement comes from the BluetoothDevice, the Fragment then tells its own ViewModel to save the new data. It is done via a Repository class.


Shared ViewModel between fragment and AndroidService
Shared ViewModel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in the shared ViewModel.

  • The Fragment observes the LiveData in its own ViewModel.


Service ViewModel
Service ViewMOdel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in its own ViewModel.

  • The Fragment observes the LiveData in its own ViewModel and the BluetoothService ViewModel.




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.




So, finally, my question is:



Where should we place our Android (Bound) Services and what is their relation with the other architectural components? Is any of these alternatives a good approach?










share|improve this question




















  • 1




    You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
    – Jeel Vankhede
    Nov 29 at 13:56










  • @JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
    – dglozano
    Nov 29 at 18:37








  • 1




    @MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
    – gnat
    Nov 30 at 8:34










  • I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
    – Martin Zeitler
    Nov 30 at 15:57












  • @gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
    – Martin Zeitler
    Nov 30 at 16:04

















up vote
11
down vote

favorite
4












I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach.



I did a lot of research, and I couldn't find any useful guideline nor tutorial. The only hint I found about where to place the Service in my app architecture is this one, from @JoseAlcerreca Medium post




Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.




According to that, I should place my Android Services on the top of my Architecture Components hierarchy, at the same level as my Activities and Fragments. That's because Android Services are part of the Android framework, so ViewModels shouldn't know about them.



Now, I will explain briefly my scenario, but only to make the panorama clearer, not because I want an answer for this specific scenario.




  • I have an Android Application that has a MainActivity with many fragments in it, all of them tied together in a BottomNavBar.

  • I have a BluetoothService bound to myActivity and one of its fragments (because I want the Service to have the same lifecycle as the Activty but I also want to interact with it directly from my fragment).

  • The fragment interacts with the BluetoothService to get two types of information:


    • Information about the state of the Bluetooth connection. Doesn't need to be persisted.

    • Data that comes from the Bluetooth Device (it is a Scale, so weight and body composition in this case). Needs to be persisted.




Here are the 3 different architectures I can think of:



LiveData inside AndroidService
LiveData inside Android Service arch




  • The LiveData with the state of the connection and with the weight
    measurements coming from the Bluetooth Device are inside the BluetoothService.

  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The Fragment observes the LiveData about the state of the connection
    and adapts the UI accordingly (for example, enable a button if the
    state is connected).

  • The Fragment observes the LiveData of the new weight measurements. If a new weight measurement comes from the BluetoothDevice, the Fragment then tells its own ViewModel to save the new data. It is done via a Repository class.


Shared ViewModel between fragment and AndroidService
Shared ViewModel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in the shared ViewModel.

  • The Fragment observes the LiveData in its own ViewModel.


Service ViewModel
Service ViewMOdel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in its own ViewModel.

  • The Fragment observes the LiveData in its own ViewModel and the BluetoothService ViewModel.




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.




So, finally, my question is:



Where should we place our Android (Bound) Services and what is their relation with the other architectural components? Is any of these alternatives a good approach?










share|improve this question




















  • 1




    You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
    – Jeel Vankhede
    Nov 29 at 13:56










  • @JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
    – dglozano
    Nov 29 at 18:37








  • 1




    @MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
    – gnat
    Nov 30 at 8:34










  • I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
    – Martin Zeitler
    Nov 30 at 15:57












  • @gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
    – Martin Zeitler
    Nov 30 at 16:04















up vote
11
down vote

favorite
4









up vote
11
down vote

favorite
4






4





I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach.



I did a lot of research, and I couldn't find any useful guideline nor tutorial. The only hint I found about where to place the Service in my app architecture is this one, from @JoseAlcerreca Medium post




Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.




According to that, I should place my Android Services on the top of my Architecture Components hierarchy, at the same level as my Activities and Fragments. That's because Android Services are part of the Android framework, so ViewModels shouldn't know about them.



Now, I will explain briefly my scenario, but only to make the panorama clearer, not because I want an answer for this specific scenario.




  • I have an Android Application that has a MainActivity with many fragments in it, all of them tied together in a BottomNavBar.

  • I have a BluetoothService bound to myActivity and one of its fragments (because I want the Service to have the same lifecycle as the Activty but I also want to interact with it directly from my fragment).

  • The fragment interacts with the BluetoothService to get two types of information:


    • Information about the state of the Bluetooth connection. Doesn't need to be persisted.

    • Data that comes from the Bluetooth Device (it is a Scale, so weight and body composition in this case). Needs to be persisted.




Here are the 3 different architectures I can think of:



LiveData inside AndroidService
LiveData inside Android Service arch




  • The LiveData with the state of the connection and with the weight
    measurements coming from the Bluetooth Device are inside the BluetoothService.

  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The Fragment observes the LiveData about the state of the connection
    and adapts the UI accordingly (for example, enable a button if the
    state is connected).

  • The Fragment observes the LiveData of the new weight measurements. If a new weight measurement comes from the BluetoothDevice, the Fragment then tells its own ViewModel to save the new data. It is done via a Repository class.


Shared ViewModel between fragment and AndroidService
Shared ViewModel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in the shared ViewModel.

  • The Fragment observes the LiveData in its own ViewModel.


Service ViewModel
Service ViewMOdel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in its own ViewModel.

  • The Fragment observes the LiveData in its own ViewModel and the BluetoothService ViewModel.




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.




So, finally, my question is:



Where should we place our Android (Bound) Services and what is their relation with the other architectural components? Is any of these alternatives a good approach?










share|improve this question















I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach.



I did a lot of research, and I couldn't find any useful guideline nor tutorial. The only hint I found about where to place the Service in my app architecture is this one, from @JoseAlcerreca Medium post




Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. A general rule of thumb is to make sure there are no android.* imports in your ViewModels (with exceptions like android.arch.*). The same applies to presenters.




According to that, I should place my Android Services on the top of my Architecture Components hierarchy, at the same level as my Activities and Fragments. That's because Android Services are part of the Android framework, so ViewModels shouldn't know about them.



Now, I will explain briefly my scenario, but only to make the panorama clearer, not because I want an answer for this specific scenario.




  • I have an Android Application that has a MainActivity with many fragments in it, all of them tied together in a BottomNavBar.

  • I have a BluetoothService bound to myActivity and one of its fragments (because I want the Service to have the same lifecycle as the Activty but I also want to interact with it directly from my fragment).

  • The fragment interacts with the BluetoothService to get two types of information:


    • Information about the state of the Bluetooth connection. Doesn't need to be persisted.

    • Data that comes from the Bluetooth Device (it is a Scale, so weight and body composition in this case). Needs to be persisted.




Here are the 3 different architectures I can think of:



LiveData inside AndroidService
LiveData inside Android Service arch




  • The LiveData with the state of the connection and with the weight
    measurements coming from the Bluetooth Device are inside the BluetoothService.

  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The Fragment observes the LiveData about the state of the connection
    and adapts the UI accordingly (for example, enable a button if the
    state is connected).

  • The Fragment observes the LiveData of the new weight measurements. If a new weight measurement comes from the BluetoothDevice, the Fragment then tells its own ViewModel to save the new data. It is done via a Repository class.


Shared ViewModel between fragment and AndroidService
Shared ViewModel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in the shared ViewModel.

  • The Fragment observes the LiveData in its own ViewModel.


Service ViewModel
Service ViewMOdel arch




  • The Fragment can trigger operations in the BluetoothService (scanDevices for example)

  • The BluetoothService updates the Bluetooth related LiveData in its own ViewModel.

  • The Fragment observes the LiveData in its own ViewModel and the BluetoothService ViewModel.




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.




So, finally, my question is:



Where should we place our Android (Bound) Services and what is their relation with the other architectural components? Is any of these alternatives a good approach?







android android-service android-architecture-components android-jetpack android-architecture






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 at 19:03

























asked Nov 19 at 20:40









dglozano

785120




785120








  • 1




    You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
    – Jeel Vankhede
    Nov 29 at 13:56










  • @JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
    – dglozano
    Nov 29 at 18:37








  • 1




    @MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
    – gnat
    Nov 30 at 8:34










  • I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
    – Martin Zeitler
    Nov 30 at 15:57












  • @gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
    – Martin Zeitler
    Nov 30 at 16:04
















  • 1




    You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
    – Jeel Vankhede
    Nov 29 at 13:56










  • @JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
    – dglozano
    Nov 29 at 18:37








  • 1




    @MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
    – gnat
    Nov 30 at 8:34










  • I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
    – Martin Zeitler
    Nov 30 at 15:57












  • @gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
    – Martin Zeitler
    Nov 30 at 16:04










1




1




You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
– Jeel Vankhede
Nov 29 at 13:56




You can assume your service as 'lifecycle aware component' in MVVM. How? You already have bounded service, bind it to *lifecycle owner*(in your case activity and one fragment you're binding to) and during any resume or start event of your lifecycle observer(you service here), call or notify your data change to your lifecycle owner. so all you need is implement LifecycleObserver interface.
– Jeel Vankhede
Nov 29 at 13:56












@JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
– dglozano
Nov 29 at 18:37






@JeelVankhede that's a nicer way of managing the binding and unbinding of my Service that I hadn't considered, thanks! However, I can't still understand how this will end up working in relation with my ViewModel and LiveData issue. You would place the Ble related LiveData in the Fragment's ViewModel? How to notify changes in between? Because the data is not available onStart or onResume, it is gathered in between.
– dglozano
Nov 29 at 18:37






1




1




@MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 30 at 8:34




@MartinZeitler when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 30 at 8:34












I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
– Martin Zeitler
Nov 30 at 15:57






I'm voting to close this question as off-topic because this it belongs to softwareengineering.stackexchange.com
– Martin Zeitler
Nov 30 at 15:57














@gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
– Martin Zeitler
Nov 30 at 16:04






@gnat just thought so, because I've linked to another answer there, in the comments of my retracted answer... and since this question is not directly related to code, it appears to be off-topic. there it might even receive better answers than on here. so far it is not yet a cross-post, but should be migrated.
– Martin Zeitler
Nov 30 at 16:04














2 Answers
2






active

oldest

votes

















up vote
1
down vote



+50










In my opinion, Service should be on same level as Activity/Fragment, because it's Framework component & not MVVM. but because of that Service doesn't implements LifecycleOwner and it's Android Framework Component, it shouldn't be treated as data source because it can be entry point to application.



So, dilemma here is that sometimes (In your case), Service acts as data source which provides data from some long running task to UI.



So what it should be in Android Architecture Component? I think you can treat it as LifecycleObserver. because, no matter what you do in background, you'll need to consider about lifecycle of the LifecycleOwner.



Why? because, we usually do bind it to LifecycleOwner (Activity/Fragments) & to do long running tasks off the UI. So, it can be treated like LifecycleObserver. In such a way we made our Service as "Lifecycle aware component" !






How you can implement it?





  1. Take your service class and implement LifecycleObserver interface to it.


  2. When you bind your service to Activity/Fragment, during your service connection of your service class, add your service to your activity as LifecycleObserver by calling method getLifecycle().addObserver(service class obj)


  3. Now, Take an interface in service class to provide callback from service to your UI and every time your data changes, check that if your service has at least on Lifecycle event create or resume to provide callback with.



In such a way, we won't require LiveData to update to from service and even no ViewModel (Why do we need it for service? We don't need configuration changes to survive on service lifecycle. And main task to VM is that to consist data between lifecycles).



Hope i made it clear for you !






share|improve this answer





















  • Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
    – dglozano
    Nov 30 at 13:01










  • Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
    – dglozano
    Nov 30 at 13:04










  • Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
    – Jeel Vankhede
    Nov 30 at 13:06












  • I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
    – dglozano
    Nov 30 at 16:09






  • 1




    No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
    – Jeel Vankhede
    Dec 2 at 4:30


















up vote
-1
down vote













How about treating your service like this?



enter image description here






share|improve this answer

















  • 1




    I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
    – MidasLefko
    Nov 27 at 8:30










  • Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
    – dglozano
    Nov 27 at 11:08












  • To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
    – dglozano
    Nov 27 at 11:18










  • Also, from Android Dev's Guide: 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.
    – dglozano
    Nov 27 at 11:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382320%2fboundservice-livedata-viewmodel-best-practice-in-new-android-recommended-arc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



+50










In my opinion, Service should be on same level as Activity/Fragment, because it's Framework component & not MVVM. but because of that Service doesn't implements LifecycleOwner and it's Android Framework Component, it shouldn't be treated as data source because it can be entry point to application.



So, dilemma here is that sometimes (In your case), Service acts as data source which provides data from some long running task to UI.



So what it should be in Android Architecture Component? I think you can treat it as LifecycleObserver. because, no matter what you do in background, you'll need to consider about lifecycle of the LifecycleOwner.



Why? because, we usually do bind it to LifecycleOwner (Activity/Fragments) & to do long running tasks off the UI. So, it can be treated like LifecycleObserver. In such a way we made our Service as "Lifecycle aware component" !






How you can implement it?





  1. Take your service class and implement LifecycleObserver interface to it.


  2. When you bind your service to Activity/Fragment, during your service connection of your service class, add your service to your activity as LifecycleObserver by calling method getLifecycle().addObserver(service class obj)


  3. Now, Take an interface in service class to provide callback from service to your UI and every time your data changes, check that if your service has at least on Lifecycle event create or resume to provide callback with.



In such a way, we won't require LiveData to update to from service and even no ViewModel (Why do we need it for service? We don't need configuration changes to survive on service lifecycle. And main task to VM is that to consist data between lifecycles).



Hope i made it clear for you !






share|improve this answer





















  • Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
    – dglozano
    Nov 30 at 13:01










  • Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
    – dglozano
    Nov 30 at 13:04










  • Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
    – Jeel Vankhede
    Nov 30 at 13:06












  • I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
    – dglozano
    Nov 30 at 16:09






  • 1




    No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
    – Jeel Vankhede
    Dec 2 at 4:30















up vote
1
down vote



+50










In my opinion, Service should be on same level as Activity/Fragment, because it's Framework component & not MVVM. but because of that Service doesn't implements LifecycleOwner and it's Android Framework Component, it shouldn't be treated as data source because it can be entry point to application.



So, dilemma here is that sometimes (In your case), Service acts as data source which provides data from some long running task to UI.



So what it should be in Android Architecture Component? I think you can treat it as LifecycleObserver. because, no matter what you do in background, you'll need to consider about lifecycle of the LifecycleOwner.



Why? because, we usually do bind it to LifecycleOwner (Activity/Fragments) & to do long running tasks off the UI. So, it can be treated like LifecycleObserver. In such a way we made our Service as "Lifecycle aware component" !






How you can implement it?





  1. Take your service class and implement LifecycleObserver interface to it.


  2. When you bind your service to Activity/Fragment, during your service connection of your service class, add your service to your activity as LifecycleObserver by calling method getLifecycle().addObserver(service class obj)


  3. Now, Take an interface in service class to provide callback from service to your UI and every time your data changes, check that if your service has at least on Lifecycle event create or resume to provide callback with.



In such a way, we won't require LiveData to update to from service and even no ViewModel (Why do we need it for service? We don't need configuration changes to survive on service lifecycle. And main task to VM is that to consist data between lifecycles).



Hope i made it clear for you !






share|improve this answer





















  • Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
    – dglozano
    Nov 30 at 13:01










  • Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
    – dglozano
    Nov 30 at 13:04










  • Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
    – Jeel Vankhede
    Nov 30 at 13:06












  • I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
    – dglozano
    Nov 30 at 16:09






  • 1




    No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
    – Jeel Vankhede
    Dec 2 at 4:30













up vote
1
down vote



+50







up vote
1
down vote



+50




+50




In my opinion, Service should be on same level as Activity/Fragment, because it's Framework component & not MVVM. but because of that Service doesn't implements LifecycleOwner and it's Android Framework Component, it shouldn't be treated as data source because it can be entry point to application.



So, dilemma here is that sometimes (In your case), Service acts as data source which provides data from some long running task to UI.



So what it should be in Android Architecture Component? I think you can treat it as LifecycleObserver. because, no matter what you do in background, you'll need to consider about lifecycle of the LifecycleOwner.



Why? because, we usually do bind it to LifecycleOwner (Activity/Fragments) & to do long running tasks off the UI. So, it can be treated like LifecycleObserver. In such a way we made our Service as "Lifecycle aware component" !






How you can implement it?





  1. Take your service class and implement LifecycleObserver interface to it.


  2. When you bind your service to Activity/Fragment, during your service connection of your service class, add your service to your activity as LifecycleObserver by calling method getLifecycle().addObserver(service class obj)


  3. Now, Take an interface in service class to provide callback from service to your UI and every time your data changes, check that if your service has at least on Lifecycle event create or resume to provide callback with.



In such a way, we won't require LiveData to update to from service and even no ViewModel (Why do we need it for service? We don't need configuration changes to survive on service lifecycle. And main task to VM is that to consist data between lifecycles).



Hope i made it clear for you !






share|improve this answer












In my opinion, Service should be on same level as Activity/Fragment, because it's Framework component & not MVVM. but because of that Service doesn't implements LifecycleOwner and it's Android Framework Component, it shouldn't be treated as data source because it can be entry point to application.



So, dilemma here is that sometimes (In your case), Service acts as data source which provides data from some long running task to UI.



So what it should be in Android Architecture Component? I think you can treat it as LifecycleObserver. because, no matter what you do in background, you'll need to consider about lifecycle of the LifecycleOwner.



Why? because, we usually do bind it to LifecycleOwner (Activity/Fragments) & to do long running tasks off the UI. So, it can be treated like LifecycleObserver. In such a way we made our Service as "Lifecycle aware component" !






How you can implement it?





  1. Take your service class and implement LifecycleObserver interface to it.


  2. When you bind your service to Activity/Fragment, during your service connection of your service class, add your service to your activity as LifecycleObserver by calling method getLifecycle().addObserver(service class obj)


  3. Now, Take an interface in service class to provide callback from service to your UI and every time your data changes, check that if your service has at least on Lifecycle event create or resume to provide callback with.



In such a way, we won't require LiveData to update to from service and even no ViewModel (Why do we need it for service? We don't need configuration changes to survive on service lifecycle. And main task to VM is that to consist data between lifecycles).



Hope i made it clear for you !







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 30 at 6:15









Jeel Vankhede

2,0512216




2,0512216












  • Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
    – dglozano
    Nov 30 at 13:01










  • Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
    – dglozano
    Nov 30 at 13:04










  • Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
    – Jeel Vankhede
    Nov 30 at 13:06












  • I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
    – dglozano
    Nov 30 at 16:09






  • 1




    No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
    – Jeel Vankhede
    Dec 2 at 4:30


















  • Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
    – dglozano
    Nov 30 at 13:01










  • Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
    – dglozano
    Nov 30 at 13:04










  • Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
    – Jeel Vankhede
    Nov 30 at 13:06












  • I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
    – dglozano
    Nov 30 at 16:09






  • 1




    No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
    – Jeel Vankhede
    Dec 2 at 4:30
















Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
– dglozano
Nov 30 at 13:01




Thanks, so far it's the best answer I think :) However, I won't mark it as the correct just yet. It is true that the service is in charge of retrieving some, but it is also keeping track of the connection status with the device. The fragment enables/disables some buttons on the UI depending on the connection status. This is something that should survive a rotation of the screen, for example. Therefore, the BluetoothService is storing that connection status in a LiveData Object and the fragment observes it to react accordingly. How would you handle that?
– dglozano
Nov 30 at 13:01












Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
– dglozano
Nov 30 at 13:04




Then about the other type of information that the BluetoothService handles, the retrieving of data, I agree with what you say :) Just to clarify, you propose that: the Activity has a reference to the Service and trigger different operations in it, and then the Service doesn't have a reference to the activity, but instead communicates the arrival of a new bunch of data using the observer pattern? Is that right?
– dglozano
Nov 30 at 13:04












Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
– Jeel Vankhede
Nov 30 at 13:06






Yes, for your LiveData purpose, use the same ViewModel of your Activity/Fragment & communicate with it using Interface callbacks to and forth to your service. You can use transformation map on livedata for that purpose.
– Jeel Vankhede
Nov 30 at 13:06














I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
– dglozano
Nov 30 at 16:09




I see your point. And what would be the pros and cons of doing that (Activity/Fragment triggers operations to Service, Service answer with callbacks, Activity/Fragment updates the related LiveData in its own ViewModel) with using a "Shared viewModel" between the Service and the Activity/Fragment? So the data doesn't go from the Service to the Activity and from there to thew ViewModel, but directly from the Service to the ViewModel
– dglozano
Nov 30 at 16:09




1




1




No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
– Jeel Vankhede
Dec 2 at 4:30




No from my point, don't use shared VM or LiveData for service. We've made our service lifecycleObserver meaning that it's now aware of activity/fragment. So , use direct callback by respecting lifecycleOwner States. No need to use mediator for service now.
– Jeel Vankhede
Dec 2 at 4:30












up vote
-1
down vote













How about treating your service like this?



enter image description here






share|improve this answer

















  • 1




    I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
    – MidasLefko
    Nov 27 at 8:30










  • Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
    – dglozano
    Nov 27 at 11:08












  • To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
    – dglozano
    Nov 27 at 11:18










  • Also, from Android Dev's Guide: 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.
    – dglozano
    Nov 27 at 11:19















up vote
-1
down vote













How about treating your service like this?



enter image description here






share|improve this answer

















  • 1




    I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
    – MidasLefko
    Nov 27 at 8:30










  • Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
    – dglozano
    Nov 27 at 11:08












  • To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
    – dglozano
    Nov 27 at 11:18










  • Also, from Android Dev's Guide: 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.
    – dglozano
    Nov 27 at 11:19













up vote
-1
down vote










up vote
-1
down vote









How about treating your service like this?



enter image description here






share|improve this answer












How about treating your service like this?



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 at 8:17









M-WaJeEh

13.7k85080




13.7k85080








  • 1




    I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
    – MidasLefko
    Nov 27 at 8:30










  • Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
    – dglozano
    Nov 27 at 11:08












  • To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
    – dglozano
    Nov 27 at 11:18










  • Also, from Android Dev's Guide: 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.
    – dglozano
    Nov 27 at 11:19














  • 1




    I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
    – MidasLefko
    Nov 27 at 8:30










  • Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
    – dglozano
    Nov 27 at 11:08












  • To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
    – dglozano
    Nov 27 at 11:18










  • Also, from Android Dev's Guide: 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.
    – dglozano
    Nov 27 at 11:19








1




1




I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
– MidasLefko
Nov 27 at 8:30




I think OP mentioned a requirement for the service to be bound to Activity to get the Activity lifecycle... OP probably doesnt want to kill battery scanning and observing bluetooth if not relevant.
– MidasLefko
Nov 27 at 8:30












Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
– dglozano
Nov 27 at 11:08






Hmmm this was my first thought. However, I feel BluetoothService is not like a webservice. It is an android component, with its own lifecycle and it is managed by Android OS. It also needs to be started passing a Context. So I do not think it can be down there. Moreover, what would be the Responsibility of "Ble Data Source"? Also, if you look the architecture from top to down, the deeper you go the more "far" you are from Android, until you "leave it completely" when you reach SQL or your API. With BluetoothService, that doesn't happen, and it feels weird.
– dglozano
Nov 27 at 11:08














To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
– dglozano
Nov 27 at 11:18




To make a parallelism, in my opinion, the equivalent of SQLite or a webservice in my scenario would be the GattServer inside my Scale. The interaction with it is handled by the Bluetooth Service, so in that sense would be the equivalent of a DAO. So everything seemed to make sense, until I read JoseAlcerreca post.
– dglozano
Nov 27 at 11:18












Also, from Android Dev's Guide: 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.
– dglozano
Nov 27 at 11:19




Also, from Android Dev's Guide: 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.
– dglozano
Nov 27 at 11:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382320%2fboundservice-livedata-viewmodel-best-practice-in-new-android-recommended-arc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out