Skip to content

NavHost

NavHost is a composable that shows the last entry of a backstack, manages saved state and provides all Android architecture components associated with the entry: Lifecycle, ViewModelStore and SavedStateRegistry. All these components are provided through CompositionLocalProvider within their corresponding owners: LocalLifecycleOwner, LocalViewModelStoreOwner and LocalSavedStateRegistryOwner.

The components are kept around until its associated entry is removed from the backstack (or until the parent entry containing the current child NavHost is removed).

The default NavHost implementation by itself doesn't provide any animated transitions, it simply jump-cuts to the next destination:

Each NavEntry from the backstack is mapped to NavHostEntry within NavHost. NavHostEntry is what actually implements LifecycleOwner, SavedStateRegistryOwner and ViewModelStoreOwner interfaces.

Usually, you don't need to interact with NavHostEntries directly, everything just works out of the box. But if you have a situation when you need to access all NavHostEntries from the current backstack, e.g. trying to access a ViewModel of neighbour entry, you could do it through the NavHostScope receiver of the contentSelector parameter.

NavHostState is a state holder of NavHost that stores and manages saved state and all Android architecture components for each entry. By default, it is automatically created by NavHost, but it is possible to create and set it into NavHost manually.

Note that you most probably don't need to use the state holder directly unless you are conditionally adding/removing NavHost to/from composition:

val state = rememberNavHostState(backstack)
if (visible) {
     NavHost(state) {
         // ...
     }
}

In this example, the state of NavHost will be properly preserved, as it is placed outside of condition.

If you do want to clear the state when NavHost is removed by condition, use NavHostVisibility/NavHostAnimatedVisibility. These composables properly clear the internal state of NavHost when the visible parameter is set to false:

NavHostVisibility(visible) {
    NavHost(backstack) {
        // ...
    }
}

You can explore the sample of NavHostVisibility usage here.