Skip to content
Moku
@moku-labs/system pages

back

The Android hardware Back button. Handlers run newest first, and exit() closes the app.

On this page

What it does

back lets you handle the hardware Back press. Only Tauri on Android has one. Everywhere else your handlers are kept and never called, and exit() answers unsupported.

How it works

A press walks the handlers from newest to oldest. The first one that returns true takes the press. If none does, the platform default runs: the webview goes back, or the app closes when there is no history.

Registering any native listener replaces the Android default. So the plugin holds the native listener only while at least one handler exists. The last remover gives Back to the system again.

Config

None.

API

Mounted at app.back.

MethodSignatureDescription
onPress(fn: () => boolean) => () => voidRegisters a handler. Local and synchronous. Returns the remover. A second call of the remover does nothing.
exit() => Promise<SystemResult<void>>Closes the app on Android. unsupported elsewhere, unavailable after stop().

A handler that throws is logged as back:subscriber-failed and counts as not taking the press.

Events

None.

Depends on

  • The core runtime plugin.
  • In the shell: @tauri-apps/api, and { name: "back" } in config.system of native. It adds one permission on Android, core:app:allow-exit.

Example

src/back.ts
import { createApp } from "@moku-labs/system";
import { backPlugin } from "@moku-labs/system/back";

let popupOpen = true;

const system = createApp({ plugins: [backPlugin] });
await system.start();

const off = system.back.onPress(() => {
  if (!popupOpen) return false;
  popupOpen = false;
  return true;
});

off();
  1. Not taken: the next handler or the platform default runs.
  • Return a boolean every time. A handler that always returns true traps the user in the app.