Back button, memory and splash screen
Make your own app handle the Android back button, free memory as screens close, and keep a loading view up until Angular draws. The apps ng build --platform generates do all three. This page shows how to do the same in yours.
All three use one native module named AngularLynxShell. A native module is Swift or Kotlin code your app registers with Lynx, which the runtime calls by name. Give it the methods you need:
The generated apps' versions are in AngularLynx's packages/native/templates, for you to copy. Work through this page with whoever maintains your native app.
Connect the Android back button
In the Android app ng build --platform android generates, back goes back a screen through Angular's Router. From the first screen, it leaves the app. Your own Android app can do the same in four steps:
- Register the
AngularLynxShellmodule with asetBackHandlerEnabled(enabled: Boolean)method. The runtime calls it withtruewhile the router has a screen to go back to, or something open on top takes back (seeLynxBackButton). It calls it withfalseotherwise. - Handle back while it's enabled, with an
OnBackPressedCallbackthat starts disabled. Android decides whether your app handles back as soon as the gesture starts. That's too early to ask JavaScript, so the runtime tells your app ahead of time. - Send the
angularLynxBackglobal event to the LynxView when the callback runs. The runtime closes what's open on top, or callsLocation.back(). - Send each step of Android's predictive back gesture as the
angularLynxBackGestureglobal event. A<lynx-stack-outlet>then moves its top page with the finger, as in the generated app, and brings it back if the gesture is canceled.
A global event is a message your native code sends to the LynxView, which the runtime listens for.
This activity and module do all four steps:
Without the module, the back button does whatever your app already does. iOS has no back button, so there's nothing to connect there.
The gesture event
The runtime reads angularLynxBackGesture's map in two shapes:
{ phase: "started" | "progressed", progress, edge }while the finger moves.progressisBackEventCompat.progress, from 0 to 1.edgeis itsswipeEdge, as"left"or"right".{ phase: "canceled" }alone, for a canceled gesture.
When the gesture's steps arrive
Your app gets the gesture's steps when:
- It uses AndroidX Activity 1.8 or later, which adds
BackEventCompatand thehandleOnBack*steps. - It runs on Android 14 or later.
- It opts in, unless it runs on Android 16 and targets API 36. Set
android:enableOnBackInvokedCallback="true"on the manifest's<application>, as the generated app does:
Without the opt-in, back still works, through handleOnBackPressed() alone. The page leaves once the gesture is done, rather than following the finger.
Free the memory screens leave behind
Lynx's JavaScript engine, which runs your Angular code, collects garbage as the app runs. But those collections leave behind much of what a screen leaves when it goes. So the app's memory grows with every screen it shows.
A full collection frees it. Lynx runs one, on every LynxView, when your app reports memory pressure with LynxEnv.trimMemory. Reporting it is up to your app.
The apps ng build --platform generates do two things, which your app can copy:
- Report the system's memory warnings, and the app leaving the screen. Off screen, a collection's pause can't show.
- Give
AngularLynxShellacollectGarbage()method that reports moderate pressure. The runtime calls it once the app has released about a screen's worth of elements and gone a second without releasing more. It calls it at most every 10 seconds. On Android, it's the same module as the back button's.
On iOS, report the warnings in your app delegate and scene delegate:
Then register the module, with config.register(AngularLynxShell.self):
On Android, report the warnings in your Application. Lynx's MemoryPressureLevel is 1 for moderate and 2 for critical:
Then add the method to the AngularLynxShell module:
Without the module, the runtime asks once, and never again.
Know when the first screen is drawn
Keep your own loading view over the LynxView until Angular has drawn its first screen.
LynxSplashScreen calls hideSplashScreen() on AngularLynxShell, with no arguments, once the page's first screen is drawn. That's at the first frame Angular commits, or when the page's provideSplashScreen() says. It's called at most once per load.
To use it:
- Give
AngularLynxShellahideSplashScreen()method. - Take your loading view down when it's called.
- Add a timeout of your own, in case it never is.
The generated apps have one you can copy:
- iOS:
ShellModule.swift, withSplashScreen.swift, which keeps a copy of the launch screen over the LynxView. - Android:
ShellModule.kt, whoseMainActivitykeeps AndroidX's splash screen up.
Without the module, nothing is called.
