From the course: Android Development Essential Training: 3 Navigation

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Handle events with LifecycleObserver

Handle events with LifecycleObserver - Android Tutorial

From the course: Android Development Essential Training: 3 Navigation

Start my 1-month free trial

Handle events with LifecycleObserver

- [Instructor] Many Android components in libraries require you to subscribe or initialize the component, and then unsubscribed or stop it. If you forget to do so, it can lead to memory leaks and subtle bugs in your app. Typically, we handle this by subscribing and unsubscribing directly in our activity or fragment. However, this often makes your code messy and difficult to maintain. To handle it better, we can rely on the life cycle components available in jet pack. There are three primary classes that we'll be working with. Life cycle, life cycle owner, and lifecycle observer. Lifecycle holds information about the life cycle state of a component and it allows other objects to observe that state. It uses two main enumerations to track the status of its associated component, event and state. The life cycle events mapped to the callback events and activities and fragments like on_create, on_start and on_destroy. The…

Contents