Router and RouterProvider
RouterProvider
By default a RouterProvider is supplied in the RootLayout.
The RouterProvider takes the following parameters,
- Routing map
- A Start Page
- A Fallback Page
- The Application Content
RouterProvider(
routing: HashMap<String, @Composable () -> Unit>,
start: @Composable () -> Unit,
fallback: @Composable () -> Unit,
children: @Composable () -> Unit
)
Router
The router can be accessed with useRouter(). The router can be used to change where in the application a user is located with either a Composable Page or a string that links to a router mapping.
@Composable
fun Page() {
val router = useRouter()
router.setLocation { Page() }
router.setLocation('some route')
router.setLocation() // Loads Start Page
}
Routes
If there are pages that does not take any parameters they can be added to the routers mapping system.
That way other applications can start those pages when they start the application activity, and a new page does not need to be created every time, instead the router reuses the old page.
val meta = Meta(
routing = HashMap<String, @Composable () -> Unit>().apply {
put('foo') { Foo() }
}
)
If another activity loads in the application, the route extras can be used to set what page the application should load.
startActivity(
Intent(
context,
App::class.java
).apply { putExtra('route', 'foo') }
)
This only works if the application builds on the RootLayout.