Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the LifecycleService and the Service class?

Tags:

android

kotlin

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?

like image 752
Tarsila Costalonga Avatar asked Jan 03 '21 23:01

Tarsila Costalonga


People also ask

Which lifecycle method do we use in service?

onStartCommand() The system calls this method whenever a component, say an activity requests 'start' to a service, using startService().

What is service life cycle service?

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

What is life cycle view owner?

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 .

What is Life Cycle owner Android?

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.


Video Answer


1 Answers

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.

like image 193
Tenfour04 Avatar answered Oct 31 '22 02:10

Tenfour04