Skip to main content

NavHost

Function NavHost 

Source
pub fn NavHost<R, F>(__arg0: NavController<R>, __arg1: F)
where R: Clone + PartialEq + 'static, F: FnMut(R) + 'static,
Expand description

Shows the screen on top of controller’s back stack — Android’s NavHost.

content receives the route and composes its screen, usually with a match, so the compiler checks that every route has one:

use cranpose_navigation::{NavHost, rememberNavController};

#[derive(Clone, PartialEq)]
enum Screen {
    Home,
    Detail { id: u32 },
}

fn App() {
    let nav = rememberNavController(Screen::Home);
    NavHost(nav, move |screen| match screen {
        Screen::Home => HomeScreen(move |id| nav.navigate(Screen::Detail { id })),
        Screen::Detail { id } => DetailScreen(id),
    });
}

Screens crossfade the way Compose’s do. The platform back gesture pops the back stack while there is a screen beneath the top one. Every entry has its own view model store: viewModel in a screen finds the screen’s view models, which are kept while other screens cover it and dropped once it is popped and has faded out.