Skip to main content

@napacitor/app

App lifecycle plugin (corresponds to @capacitor/app).

Installation​

To use npm:

npm install @napacitor/app

To use yarn:

yarn add @napacitor/app

Sync native files:

npx nap sync

Usage​

import { App } from '@napacitor/app';

const { isActive } = await App.getState();
const { value: language } = await App.getAppLanguage(); // 'zh-CN'
const { url } = await App.getLaunchUrl();

App.addListener('appStateChange', ({ isActive }) => console.log('active:', isActive));
App.addListener('pause', () => console.log('App went to background'));
App.addListener('resume', () => console.log('App came to foreground'));
App.addListener('appUrlOpen', ({ url }) => console.log('Deep link:', url));
App.addListener('backButton', ({ canGoBack }) => console.log('Back button'));

// Exit the app (native semantics only on Windows/macOS; Android/iOS discourage programmatic exit)
// await App.exitApp();

// Remove all listeners
await App.removeAllListeners();

How It Works​

Wraps the APIs described in the .NET MAUI Window lifecycle documentation: after the first lifecycle listener is registered (or the first getState call), the main Window's Resumed / Stopped / Backgrounding events are lazily hooked up and state changes are dispatched via appStateChange / pause / resume. exitApp maps to Application.Quit (native semantics only on Windows/macOS); getAppLanguage reads CultureInfo.CurrentUICulture.

Deep links (appUrlOpen / getLaunchUrl) and the Android back button (backButton) have no cross-platform subscribable events in MAUI; the host app must forward them to the plugin's static entry points from platform code:

// App.xaml.cs (deep links on all platforms)
protected override void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
Napacitor.Plugins.App.AppPlugin.NotifyAppLink(uri);
}

// Platforms/Android/MainActivity.cs (Android back button only)
public override void OnBackPressed()
{
if (!Napacitor.Plugins.App.AppPlugin.NotifyBackButton())
{
base.OnBackPressed(); // Default behavior when no JS listener is registered
}
}

NotifyBackButton returns true when a JS listener is registered (the host should intercept this back press); NotifyAppLink only records the link when the plugin instance has not been created yet, so getLaunchUrl can still read it.

Permissions​

Android​

  • Deep links: the MainActivity in AndroidManifest.xml must declare an intent-filter (custom scheme), and MainActivity must override OnBackPressed to forward the back button (see How It Works above).
  • Back button: to listen for backButton, after forwarding, JS decides whether to exit (e.g. calling App.exitApp(); note that Android discourages programmatic exit and Application.Quit has no native semantics on Android).

iOS​

Info.plist must declare CFBundleURLTypes to register a custom URL scheme, and App.xaml.cs must forward events (see How It Works above). iOS discourages programmatic exit; exitApp has no native semantics on iOS.

Windows​

Packaged apps declare the protocol in Package.appxmanifest; Application.Quit works on Windows.

API​

exitApp()​

Exits the app. Native semantics only on Windows/macOS; Android/iOS discourage programmatic exit (HIG/Play policies), so force-exiting requires host platform code.

Returns: Promise<void>

getState()​

Gets the current app state (also lazily hooks up lifecycle events).

Returns: Promise<AppState>

getLaunchUrl()​

Gets the deep link that launched the app. Requires the host to register a URL scheme and forward it in App.xaml.cs; url is an empty string when none has been received.

Returns: Promise<AppUrl>

getAppLanguage()​

Gets the app language (CultureInfo.CurrentUICulture, BCP-47 style such as 'zh-CN'; falls back to the ISO two-letter code when unrecognized).

Returns: Promise<AppLanguage>

addListener('appStateChange', listenerFunc)​

Dispatched when the app moves between foreground and background (payload is AppState).

ParamType
eventName'appStateChange'
listenerFunc(state: AppState) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('pause', listenerFunc)​

Dispatched when the app goes to the background.

ParamType
eventName'pause'
listenerFunc() => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('resume', listenerFunc)​

Dispatched when the app returns to the foreground.

ParamType
eventName'resume'
listenerFunc() => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('appUrlOpen', listenerFunc)​

Dispatched when the app is opened/resumed via a deep link. Requires the host to call AppPlugin.NotifyAppLink(uri) in App.xaml.cs's OnAppLinkRequestReceived, otherwise it never fires.

ParamType
eventName'appUrlOpen'
listenerFunc(data: AppUrl) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('backButton', listenerFunc)​

Dispatched when the Android hardware back button is pressed. Requires the host to call AppPlugin.NotifyBackButton() in MainActivity.OnBackPressed, otherwise it never fires. Registering this event intercepts the current back press; call App.exitApp() to exit the app.

ParamType
eventName'backButton'
listenerFunc(data: BackButtonResponse) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

removeAllListeners()​

Removes all listeners of this plugin.

Returns: Promise<void>

Interfaces​

AppState​

PropTypeDescription
isActivebooleanWhether the app is active (foreground and interactive).

AppLanguage​

PropTypeDescription
valuestringApp language (BCP-47 style, such as 'zh-CN'; falls back to the ISO two-letter code when unrecognized).

AppUrl​

PropTypeDescription
urlstringDeep link URL; empty string when none has been received.

BackButtonResponse​

PropTypeDescription
canGoBackbooleanWhether the WebView history can go back (false when the host provides none).