46.2 Creating a Lifecycle Observer
As previously discussed, activities and fragments already implement the LifecycleOwner interface and are ready to be observed by other objects. To see this in practice, the next step in this tutorial is to add a new class to the project that will be able to observe the MainFragment instance.
To add the new class, right-click on app -> java -> com.ebookfrenzy.lifecycledemo in the Project tool window and select New -> Kotlin File/Class... from the resulting menu. In the New Class dialog, name the class DemoObserver, select Class from the list and press the keyboard Return key to create the DemoObserver.kt file. The new file should automatically open in the editor where it will read as follows:
package com.ebookfrenzy.lifecycledemo
class DemoObserver {
}
Remaining in the editor, modify the class file to declare that it will be implementing the LifecycleObserver interface:
package com.ebookfrenzy.lifecycledemo
...