36.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.
public class SampleObserver implements 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.
getLifecycle().addObserver(new SampleObserver());
An observer may also be removed from the Lifecycle object at any time if it no longer needs to track the lifecycle state.
Figure 36-1 illustrates the relationship between the key elements that provide lifecycle awareness:
Figure 36-1