Skip to content

Navigation

Navigation in zero-native is native-driven (AndroidX Navigation 3 on Android, UINavigationController on iOS) with full TypeScript route safety, deep links, bottom tabs, back handlers, and automatic process-death restoration.


Declare all screens in routes.ts using defineRoutes. The compiler reads this file at build time so native can draw the first screen’s skeleton before JS even starts.

app/src/routes.ts
import { defineRoutes } from "@zeronative/core"
import { HomeScreen } from "./HomeScreen"
import { StoreScreen } from "./StoreScreen"
import { CheckoutScreen } from "./CheckoutScreen"
export const routes = defineRoutes({
home: {
path: "/",
screen: HomeScreen,
tabs: ["home", "offers", "profile"] as const,
},
store: {
path: "/store/:storeId",
screen: StoreScreen,
deepLink: true,
},
checkout: {
path: "/checkout/:sessionId",
screen: CheckoutScreen,
safeArea: "all",
keyboard: "resize",
restore: false, // don't reopen a completed checkout screen after process death
secure: true, // hides the screen from screenshots & screen recording
},
})
// Enables typed nav.push("store", { storeId: "123" }) across your app
declare module "@zeronative/core" {
interface Register {
routes: typeof routes
}
}

Start the app in main.ts:

import { installRuntime, startApp } from "@zeronative/core"
import { routes } from "./routes"
installRuntime()
startApp(routes)
  • path — URL pattern ("/" is the default home route; supports :param segments). Route params are JSON-serialized, so pass IDs rather than large objects.
  • deepLink?: boolean — opt-in per route for external URLs and notifications.
  • safeArea?: "all" | "none" | { top?: boolean; bottom?: boolean } — native inset handling (default "all").
  • statusBar?: "dark" | "light" — status and navigation bar icon color (default "dark").
  • keyboard?: "resize" | "none" — native IME resize behavior (default "resize").
  • transition?: "push" | "modal" | "fade" | "none" — screen transition style (default "push").
  • restore?: boolean — whether to reopen this screen after background process death (default true).
  • secure?: boolean — hides the screen from screenshots and screen recording while it is visible: FLAG_SECURE on Android; on iOS captures show it blank and the app switcher shows a plain cover (see Platform Differences).
  • transparent?: boolean — keeps the screen below visible underneath (e.g., transparent modal overlays).
  • tabs?: readonly string[] — declares tab names for useTabs(routeName).
  • native?: string — routes directly to a registered native plugin screen (see Plugins).

Import nav from @zeronative/core:

import { nav } from "@zeronative/core"
// Push a new screen with typed params
nav.push("store", { storeId: "42" })
// Replace the current screen
nav.replace("checkout", { sessionId: "abc" })
// Reset the entire stack (bottom first)
nav.reset([{ name: "home" }])
// Go back one screen
nav.back()

useFocus(fn) runs fn() (with no arguments) each time the screen becomes the top screen; if fn returns a cleanup function, it runs when the screen loses focus:

import { useFocus } from "@zeronative/core"
export function HomeScreen() {
useFocus(() => {
refreshFeed()
return () => {
// runs when another screen opens on top or this screen closes
}
})
// ...
}

(React Navigation aliases useNavigation(), useRoute(), and useFocusEffect() are also exported from @zeronative/core for easier porting.)


Intercept hardware back presses (Android) or confirm before leaving a critical screen with useBack.

Important: Registering a useBack handler disables the native predictive-back gesture on that screen so JS can decide. Register it inside a conditional part so it is active only while you actually need to guard the screen:

import { useBack } from "@zeronative/core"
function PaymentBackGuard(props: { onConfirmExit: () => void }) {
useBack(() => {
props.onConfirmExit()
return true // handled — keep the screen open
})
return null
}
export function CheckoutScreen() {
return (
<Column style={{ flex: 1 }}>
{paymentInProgress() && <PaymentBackGuard onConfirmExit={showConfirmExitSheet} />}
{/* ... */}
</Column>
)
}
  • Screens without a useBack handler pop natively with zero JS round-trip (with a 300 ms native fallback if JS is busy), enabling smooth Android predictive back gestures and iOS swipe-back. Screens with a handler wait for JS (guarded by the JS hang time limit).

Declare tabs: ["home", "offers", "profile"] on your route in routes.ts, then call useTabs("<routeName>") inside that screen:

import { useTabs } from "@zeronative/core"
import { Column, Row, Tabs, Pressable, Text } from "@zeronative/ui"
export function HomeScreen() {
const tabs = useTabs("home")
return (
<Column style={{ flex: 1 }}>
<Tabs selected={tabs.index()} style={{ flex: 1 }}>
<Column style={{ flex: 1 }}>{tabs.seen("home") && <HomeTab />}</Column>
<Column style={{ flex: 1 }}>{tabs.seen("offers") && <OffersTab />}</Column>
<Column style={{ flex: 1 }}>{tabs.seen("profile") && <ProfileTab />}</Column>
</Tabs>
<Row style={s.tabBar}>
{tabs.names.map((name) => (
<Pressable key={name} style={{ flex: 1 }} onPress={() => tabs.select(name)}>
<Text style={{ fontWeight: tabs.selected() === name ? 700 : 400 }}>{name}</Text>
</Pressable>
))}
</Row>
</Column>
)
}
  • tabs.seen(name) ensures each tab is lazily mounted only when first visited, then kept alive in memory without drawing frames while hidden.
  • Use tabs.onFocus(name, fn) for tab-specific focus effects. Pressing Back on a non-first tab returns to the first tab automatically.

Mark any route with deepLink: true in routes.ts, and configure your app’s custom URL scheme / Android intent-filter / iOS Associated Domains in the native host so the OS delivers links to your app.

  • Path params (/store/:storeId) and query params (?ref=promo) are merged into the screen’s props (path params win on collision).
  • Handle unrecognized URLs via startApp(routes, { onUnmatchedLink: (link) => ... }) or intercept links before navigation with onLink.

6. Process-Death Restoration & restorableSignal

Section titled “6. Process-Death Restoration & restorableSignal”

Mobile OSes routinely kill background apps (especially on low-RAM Android phones when the user switches to a UPI or banking app).

  • Back stack & route params are saved natively and restored automatically when the OS recreates your app (not when the user explicitly swipes the app away).
  • restorableSignal(key, initial) saves screen-local state alongside the back-stack entry so form fields or checkout state survive an OS kill:
import { restorableSignal } from "@zeronative/core"
export function CheckoutScreen() {
const note = restorableSignal("note", "")
// Survives background process death; resets when the user leaves normally.
}
  • Limits: Keep values small (4,000-character limit), non-secret, and serializable JSON. Restored values apply to the same bundle version only.
  • For critical financial or idempotent flows, pair this with FlowStore (see Data & Networking).

Holding the native splash (Splash.holdUntil)

Section titled “Holding the native splash (Splash.holdUntil)”

If your first screen needs a quick token check or cached config before appearing, hold the OS splash screen cleanly (with a built-in safety timeout so a slow network never hangs startup):

import { Splash } from "@zeronative/core"
Splash.holdUntil(loadInitialSession(), { maxMs: 1500 })

Pass named components imported from .tsx files (don’t write JSX inside main.ts) to startApp:

app/src/main.ts
import { installRuntime, startApp } from "@zeronative/core"
import { routes } from "./routes"
import { OfflineBanner } from "./OfflineBanner"
import { GlobalOverlay } from "./GlobalOverlay"
installRuntime()
startApp(routes, {
top: OfflineBanner,
overlay: GlobalOverlay,
})