Keep each tab alive
Switch tabs without rebuilding them, as a native tab bar does. Put <lynx-tab-outlet> where <router-outlet> was:
Why
With <router-outlet> beside a Tab Bar or Dock, switching tabs destroys the tab you leave. The next one is built from scratch: its components, its elements, and its native views.
So a <canvas> sets up its WebGL context again, and a <webview> loads its page again. A native tab bar keeps each tab once you've seen it, and <lynx-tab-outlet> does the same.
How it works
- Each route under it is a tab.
- It's a router outlet, with
<router-outlet>'s inputs and outputs. It works with child routes, guards, resolvers, lazy routes,withComponentInputBinding(), routeresourcesand route error boundaries. - The tab bar and the dock drive it as they drive
<router-outlet>. - The first visit builds a tab, and switching away keeps it.
- The tab you leave is hidden: out of sight, out of screen readers' reach, and out of the way of taps.
- Switching back shows it as you left it, without building anything. It keeps its scroll positions, its native views, and its
<lynx-stack-outlet>with the screens pushed on it.
A tab's item goes back to where you left the tab
A navigation to a hidden tab's own URL (/library, what its item links to) goes to the URL the tab was at (/library/album/3).
- From the tab itself, the same link goes to
/library. That pops its stack to its first screen, as tapping the selected tab does on iOS. - Any other URL goes where it says: a deeper one (
/library/album/7), or one with a query. - A navigation whose
infois{ stack: 'root' }goes where it says too, and starts the tab's stack again at that URL.
Back goes through a tab's screens first
When a tab comes back with a stack, its screens' history entries come back before the current one. So Back, Android's back button and a swipe go to the screen under the top one, then on to the tab you came from.
Pause a hidden tab
A hidden tab's components keep running. Inject LynxTabPage and read its active() signal to pause what only matters while the tab is shown, such as a 3D scene's frames or a poll.
The screens of a stack in a hidden tab say so too: their LynxStackPage.active() is false.
Lazy-load a tab's routes
A route with only loadChildren is a tab under its own path, when its loaded routes start with the tab's component on '':
keep, prebuild, and its route's keepAlive and prebuild data name it library, as its tab bar item does.
Choose which tabs to keep
Every tab you've shown is kept, bounded by the number of tabs. To let one go when you leave it:
- Give its route
data: { keepAlive: false }. - Or list the paths of the tabs to keep:
<lynx-tab-outlet [keep]="['home', 'library']" />.
Leaving the outlet's route, as signing out does, destroys every tab in it.
Build tabs ahead
Build tabs before they're first shown, so the first tap on each is as quick as coming back to it:
Or put data: { prebuild: true } on a tab's route.
- When: once the first tab is drawn, the outlet builds them one at a time, each between frames.
- Hidden: it hides them as it hides a tab you've left.
- What each gets: its component and the screens its URL leads to (its stack's first screen), with their guards and resolvers run. Its
LynxTabPage.active()isfalse. - Nothing else moves: the URL, the history and the tab bar's selection stay put.
- Tapping a tab while it's being built builds it once.
Which tabs can be built ahead
The outlet builds the tabs of the routes beside the one shown that have:
- a path without a parameter, so not
item/:id - a component, or children that start with the tab's component on
''
It doesn't build a tab it doesn't keep. In development, it warns in the console of a tab it's asked for and can't build.
When a tab fails
With Angular's route error boundaries, a tab that throws shows its route's error component in its place:
- A tab showing its error component isn't kept. Switching back to it builds it again, as
<router-outlet>does. - A hidden tab keeps running, so it can fail too. Its error component waits there, and the tab is built again when you switch back to it. That error component gets only
error, even withwithComponentInputBinding():<router-outlet>binds the route's params and data to its one screen, and the router is on the tab shown. Make the error component's route inputs optional. - A tab that fails as it's built ahead is left out, and its error goes to the app's
ErrorHandler, not to the boundary's error component oronError. Angular has nothing that builds a route before you go to it: its boundary acts for the route the router is on, and aRedirectCommandfrom a tab you haven't opened would take the app somewhere you didn't ask to go. Once it's built, the boundary catches its errors as it does a tab you've shown. To see the error component, open the tab rather than building it ahead.
Good to know
- A plain
<router-outlet>in a tab builds its screen again when the tab comes back, as it does everywhere: the router leaves its route as the tab hides. Use<lynx-stack-outlet>for a tab's screens, or another<lynx-tab-outlet>for tabs inside a tab, to keep them with it. - The tab outlet adds a guard to each tab's route, to take a tab bar back where the tab was left. It's on the router's copy of the route, so your
Routesstay as you wrote them. The guard does show inrouter.config. - A stack inside a screen of another stack is kept too, while a screen pushed over it covers it.
Tabs built ahead
- Nothing in a tab built ahead has appeared until the tab is shown. It's off screen, so
binduiappearwaits for the first tap. - Something an app makes only for what's on screen is still made then, such as a
<webview>made onbinduiappear. To have it ready, make it for what the tab shows first, without waiting. - The routes above a tab built ahead run their guards and resolvers again, as a navigation to the tab from elsewhere in the app does.
- The tab's own guards and resolvers run again when you tap it, as they do for any tab you come back to.
Related
LynxTabOutletAPI- Slide screens in and out: a stack of screens in each tab
- Routing and navigation: set up, links, going back
- Tab Bar and Dock: the bars that drive the tab outlet
