Android Jetpack: Lifecycle-aware component (LifecycleEventObserver).

Vinod Baste
CodeX
Published in
4 min readMar 20, 2022

--

image Source: google images

Lifecycle-aware components, such as activities and fragments, take action in response to a change in the lifecycle state of another component. These components aid in the creation of more well-organized and, in many cases, lighter-weight code that is easier to maintain.

Implementing the actions of dependent components in the lifecycle methods of activities and fragments is a popular practice. This approach, on the other hand, results in a disorganized code base and a high number of mistakes. You may shift the code of dependent components out of the lifecycle methods and into the components themselves by utilizing lifecycle-aware components.

Consider the following scenario: we have an activity that displays the device’s position on the screen. The following is an example of a common implementation:

Even though this sample appears to work, in a real project, you’ll end up with too many calls that manage the UI and other components in response to the current lifecycle state. Lifecycle methods, such as onStart() and onStop(), include a significant amount of code when managing several components, making them difficult to maintain.

Furthermore, there is no guarantee that the component will begin before the activity or fragment is terminated. This is especially true if a long-running action is required, such as a configuration check in onStart (). This can result in a race scenario in which the onStop() function completes before the onStart() method, causing the component to stay alive longer than necessary.

What if I told you that it’s possible to make this more simple to implement?

Let's see how with lifeCycle components.

  • LifecycleEvents (Deprecated): a deprecated method and if you have already implemented it, let's see how to set it up to use LifecycleEventObserver

Android Studio identified all Lifecycle events as deprecated after upgrading the lifecycle library to 2.4.0.

It’s deprecated since you should now use Java 8 and implement the DefaultLifecycleObserver interface. Because Java 8 permits interfaces to have default implementations, they created DefaultLifecycleObserver, which has empty implementations of all the methods so you only have to override the ones you need.

Change your class to implement DefaultLifecycleObserver and your functions to override the necessary DefaultLifecycleObserver functions in your case. You must change your Gradle build files if your project does not yet use Java 8. Put these in the android block in your module's build.gradle:

build.gradle(Module)

And that's it now that we are ready to use LifecycleEventObserver.

  • LifecycleEventObserver: is a class that stores information about a component’s lifecycle state (such as an activity or a fragment) and allows other objects to view it.

To monitor the lifespan status of its related component, LifecycleEventObserver employs two major enumerations:

The Android activity lifecycle is made up of states and events.
  • Event: The framework’s and the Lifecycle class’s lifecycle events are dispatched. The callback events in activities and fragments correspond to these occurrences.
  • State: A class can keep track of a component’s lifespan.

Let’s have a look by creating LifecycleEventObserver and overriding methods like onCreate, onStart, and so on. Then, as illustrated in the following example, you can add an observer by executing the Lifecycle class’s addObserver() function and supplying an instance of your observer:

  • LifecycleOwner: The LifecycleOwner interface is a single method interface that indicates whether or not a class has a Lifecycle. It just has one method, getLifecycle(), which the class must implement.

This interface decouples the ownership of a Lifecycle from specific classes like Fragment and Activity, allowing you to write components that interact with it.

Let’s add an observer by calling the addObserver() method of the Lifecycle class and passing an instance of observer

And that's all, now the code should be ready to run and provide the required output.

Thank you for taking the time to read this article. If you found this post to be useful and interesting, please clap and recommend it.

If I got something wrong, mention it in the comments. I would love to improve.

--

--