The LifecycleObserver class is part of Google's Architecture Components for Android. In this video, learn how to create and use a LifecycleObserver class to handle lifecycle events.
- [Instructor] I've previously shown … how to handle activity lifecycle events … with code in the activity. … Google offers a more sophisticated approach … to this task though … using a class called a lifecycle observer. … The goal is to slim down the activity … and move code that reacts to life cycle events … to a special class … that doesn't depend on specific callback functions. … The first step is to create a class … that implements an interface named Lifecycle Observer. … I'll create a new Kotlin class, … that I'll name MyLifecycleObserver. … I'll set the kind to Class, and I'll create it. … Now I'll indicate that I'm implementing that interface … with a colon and the interface name LifecycleObserver. … This interface doesn't have any required functions. … Instead it has an annotation called OnLifeCycleEvent. … For each event that you want to handle, add the annotation, … and then in a pair of parentheses, pass the constant … indicating which lifecycle event you want to handle. …
Looking for study partners?Join the Associate Android Developer Exam study group
The Android app architecture constantly evolves. The introduction of the Kotlin programming language, new form factors such as Chromebooks, and reusable architecture components have made it faster and easier than ever to build high-quality Android apps. It is imperative that developers understand what the latest tools and recommendations are and how to use them to build out their applications. In this course, instructor David Gassner explains how to build an Android app that uses Kotlin instead of Java, add navigation functionality, and leverage the most useful architecture components such as LifecycleObserver, ViewModel, and LiveData, to create apps that are robust and easy to maintain over time.
- Handling lifecycle events
- Handling view events
- Updating display from new data
- Adding ViewModels to activities
- Subscribing to changes in LiveData values
- Adding a new launcher activity
- Starting activities with explicit intents
- Sharing data with implicit intents
- Adding a navigation drawer
- Adding a header to a navigation drawer
- Handling navigating drawer menu events