Other Lifecycle Owners

Activities and fragments are not the only things with lifecycles. The Architecture Components also support other forms of lifecycle owner:

LifecycleService

If you have a class that extends Service, you can replace it with LifecycleService and get a service that is a LifecycleOwner. Four of the six lifecycle events are honored:

This Lifecycle Event… Is Triggered When…
ON_CREATE the service is created
ON_START when the service is first started or bound to
ON_RESUME unused
ON_PAUSE unused
ON_STOP when the service is destroyed
ON_DESTROY also when the service is destroyed

Of note, LifecycleService does not attempt to model binding/unbinding as a lifecycle (e.g., calling ON_STOP when the service is unbound and has no more active bindings).

However, most services do not directly inherit from Service. Instead, they extend IntentService or JobService or any one of dozens of other specialized service implementations. Few, if any, of those will extend LifecycleService, as most of them come from the core framework, which cannot depend on libraries like the Architecture Components.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.