For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/embedding/shell-module.md.

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:

MethodWhat forPlatforms
setBackHandlerEnabled(enabled)The Android back buttonAndroid
collectGarbage()Freeing memoryiOS and Android
hideSplashScreen()Hiding your loading viewiOS and Android

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:

  1. Register the AngularLynxShell module with a setBackHandlerEnabled(enabled: Boolean) method. The runtime calls it with true while the router has a screen to go back to, or something open on top takes back (see LynxBackButton). It calls it with false otherwise.
  2. Handle back while it's enabled, with an OnBackPressedCallback that 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.
  3. Send the angularLynxBack global event to the LynxView when the callback runs. The runtime closes what's open on top, or calls Location.back().
  4. Send each step of Android's predictive back gesture as the angularLynxBackGesture global 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:

class MainActivity : ComponentActivity() {
    private val lynxView by lazy {
        LynxViewBuilder().apply {
            // Your loaders, as in Lynx's integration guide.
            registerModule("AngularLynxShell", AngularLynxShell::class.java, this@MainActivity)
        }.build(this)
    }

    // Disabled until the router has history, so back leaves the screen.
    val backHandler = object : OnBackPressedCallback(false) {
        // The predictive back gesture, as the finger moves. Android calls
        // these only for the gesture, not for a button or key press.
        override fun handleOnBackStarted(backEvent: BackEventCompat) {
            sendBackGesture("started", backEvent)
        }

        override fun handleOnBackProgressed(backEvent: BackEventCompat) {
            sendBackGesture("progressed", backEvent)
        }

        override fun handleOnBackCancelled() {
            sendBackGesture("canceled", null)
        }

        // A finished gesture, or a press of back.
        override fun handleOnBackPressed() {
            lynxView.sendGlobalEvent("angularLynxBack", JavaOnlyArray())
        }
    }

    // { phase, progress, edge }, or { phase: "canceled" } alone, which has
    // no BackEventCompat.
    private fun sendBackGesture(phase: String, backEvent: BackEventCompat?) {
        val event = JavaOnlyMap()
        event.putString("phase", phase)
        if (backEvent != null) {
            event.putDouble("progress", backEvent.progress.toDouble())
            event.putString(
                "edge",
                if (backEvent.swipeEdge == BackEventCompat.EDGE_RIGHT) "right" else "left",
            )
        }
        lynxView.sendGlobalEvent("angularLynxBackGesture", JavaOnlyArray().apply { pushMap(event) })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onBackPressedDispatcher.addCallback(this, backHandler)
        // Show lynxView and load your bundle, as before.
    }
}

class AngularLynxShell(context: Context, param: Any?) : LynxModule(context, param) {
    private val activity = param as MainActivity

    // Lynx calls native modules off the main thread.
    @LynxMethod
    fun setBackHandlerEnabled(enabled: Boolean) {
        activity.runOnUiThread { activity.backHandler.isEnabled = enabled }
    }
}

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. progress is BackEventCompat.progress, from 0 to 1. edge is its swipeEdge, 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 BackEventCompat and the handleOnBack* 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:
<application
    android:enableOnBackInvokedCallback="true"
    ...>

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:

  1. Report the system's memory warnings, and the app leaving the screen. Off screen, a collection's pause can't show.
  2. Give AngularLynxShell a collectGarbage() 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:

func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
  LynxEnv.sharedInstance().trimMemory(.critical)
}

func sceneDidEnterBackground(_ scene: UIScene) {
  LynxEnv.sharedInstance().trimMemory(.moderate)
}

Then register the module, with config.register(AngularLynxShell.self):

final class AngularLynxShell: NSObject, LynxModule {
  static var name: String { "AngularLynxShell" }
  static var methodLookup: [String: String] {
    ["collectGarbage": NSStringFromSelector(#selector(collectGarbage))]
  }

  override init() { super.init() }
  init(param: Any) { super.init() }

  // Lynx calls native modules off the main thread.
  @objc func collectGarbage() {
    DispatchQueue.main.async { LynxEnv.sharedInstance().trimMemory(.moderate) }
  }
}

On Android, report the warnings in your Application. Lynx's MemoryPressureLevel is 1 for moderate and 2 for critical:

override fun onTrimMemory(level: Int) {
    super.onTrimMemory(level)
    val critical = level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
    LynxEnv.inst().trimMemory(if (critical) 2 else 1)
}

Then add the method to the AngularLynxShell module:

@LynxMethod
fun collectGarbage() {
    Handler(Looper.getMainLooper()).post { LynxEnv.inst().trimMemory(1) }
}

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:

  1. Give AngularLynxShell a hideSplashScreen() method.
  2. Take your loading view down when it's called.
  3. Add a timeout of your own, in case it never is.

The generated apps have one you can copy:

  • iOS: ShellModule.swift, with SplashScreen.swift, which keeps a copy of the launch screen over the LynxView.
  • Android: ShellModule.kt, whose MainActivity keeps AndroidX's splash screen up.

Without the module, nothing is called.