37.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 -> Java Class... from the resulting menu. In the Create New Class dialog, name the class DemoObserver and click on the OK button to create the DemoObserver.java file. The new file should automatically open in the editor where it will read as follows:
package com.ebookfrenzy.lifecycledemo;
public class DemoObserver {
}
Remaining in the editor, modify the class file to declare that it will be implementing the LifecycleObserver interface:
package com.ebookfrenzy.lifecycledemo;
import androidx...