Activity and View Model Lifecycles Demo App
Simple app to demonstrate Android lifecycle in Activity and View Model using debug logging
The app has 2 screens (First Screen and Second Screen) implemented using simple compose navigation.
Navigating between screens has no impact on the activity life cycles. However, it may impact View Model lifecycle depend on where you instantiate the ViewModel
.
Let’s first look at activity lifecycle first.
Implement DefaultLifecycleObserver
This is the official activity lifecycle diagram, indicating when these lifecycle event callbacks are called. For example, before activity goes into CREATED state, onCreate()
event callback is called. This applies to the rest of the lifecycle states.
Please note that
onCreate()
and the rest are lifecycle events and not lifecycle states.
In order to demonstrate the activity lifecycle, you need to implement DefaultLifecycleObserver
interface. Then, you override all the functions and print out…