45.3 Lifecycle Observers
In order for a lifecycle-aware component to observe the state of a lifecycle owner it must implement the LifecycleObserver interface and contain event listener handlers for any lifecycle change events it needs to observe.
class DemoObserver: LifecycleObserver {
// Lifecycle event methods go here
}
An instance of this observer class is then created and added to the list of observers maintained by the Lifecycle object.
lifecycle.addObserver(DemoObserver())
An observer may also be removed from the Lifecycle object at any time if it no longer needs to track the lifecycle state.
Figure 45-1 illustrates the relationship between the key elements that provide lifecycle awareness:
Figure 45-1