I have been reading the documentation on Android Developer page, but besides the fact that one extends from Service and implements LifecycleOwner and the other extends ContextWrapper, I have not been able to understand when to use one or the other.. Do both of them serves the same purpose?
onStartCommand() The system calls this method whenever a component, say an activity requests 'start' to a service, using startService().
A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. 2. Bound. A service is bound when an application component binds to it by calling bindService ...
LifecycleOwner is a single method interface that denotes that the class has a Lifecycle . It has one method, getLifecycle() , which must be implemented by the class. If you're trying to manage the lifecycle of a whole application process instead, see ProcessLifecycleOwner .
ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.
LifecycleService has additional functionality, so it's preferred if you need that functionality.
A LifecycleOwner allows other classes to subscribe to its lifecycle state. For example, some component in your application could automatically tear itself down, free up native memory, unregister listeners, etc. by subscribing to the Service's destroyed lifecycle stage. Then your Service code wouldn't have to worry about it.
You also have access to lifecycleScope
in a LifecycleService, and that can be used to start coroutines that will automatically be cancelled when the Service is stopped.
ViewModel and LiveData are designed around the use of lifecycles, so you could theoretically use them with a LifecycleService, but that would probably be counter-productive since they are intended for UI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With