Skip to content

Instantly share code, notes, and snippets.

@AndroidPoet
Created April 17, 2026 17:28
Show Gist options
  • Select an option

  • Save AndroidPoet/440dadc93c66bcaabf585d37107f3ecf to your computer and use it in GitHub Desktop.

Select an option

Save AndroidPoet/440dadc93c66bcaabf585d37107f3ecf to your computer and use it in GitHub Desktop.
@Composable
fun SyncWithBrowserHistory(navController: NavController) {
val currentRoute = navController.currentBackStackEntryAsState().value?.toRoute()
LaunchedEffect(currentRoute) {
val url = routeToUrl(currentRoute)
window.history.pushState(null, "", url)
}
LaunchedEffect(Unit) {
window.addEventListener("popstate") {
val path = window.location.pathname
val route = parseUrlToRoute(path)
navController.navigate(route) {
popUpTo(navController.graph.startDestination) {
inclusive = true
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment