LifecycleController

A class which is tied to every Route's destination that itself is SavedStateRegistryOwner&ViewModelStoreOwner.

This helps to incorporate scoped ViewModels within a particular destination. Since it also implements methods to handle SavedStateRegistry, we can save state in a Bundle which will be tied to the Lifecycle of the associated destination for eg: SavedStateHandle's data will be properly restored on process death. The same also carries for any rememberSaveables used within the scope of the destination. Saving state happens when the activity's onSaveInstanceState() is called & restoring happens when the destination is recreated from the restored backstack.

ViewModel instances on configuration change are same which is why we keep static references to this class in LifecycleControllerStore. The mechanism is very similar to how fragment retains ViewModel instances when it is recreated due to configuration change.

The class does implement a LifecycleOwner which we might need to clear ViewModelStore on onDestroy() or to correctly trigger saving states through SavedStateRegistry. One can listen to the lifecycle changes to the destination Route by adding a LifecycleObserver, but to still give you a clarity how lifecycle event affects during navigation here is an example.

navigator.navigateTo(A)
// A -> onCreate -> onStart -> onResume
// backstack = [A]

navigator.navigateTo(B)
// A -> onPause -> onStop
// B -> onCreate -> onStart -> onResume
// backstack = [A, B]

navigator.goBack()
// B -> onPause -> onStop -> onDestroy
// A -> onStart -> onResume
// backstack = [A]

Functions

Link copied to clipboard
open override fun getLifecycle(): Lifecycle
Link copied to clipboard
open override fun getSavedStateRegistry(): SavedStateRegistry
Link copied to clipboard
open override fun getViewModelStore(): ViewModelStore