Skip to main content

App Lifecycle

App Lifecycle

Handle app state changes, deep links, and back button

Tags: background foreground deeplink url backbutton

View Demo

The useAppLifecycle composable provides access to app lifecycle events including state changes (foreground/background), URL/deep link handling, and Android back button interception.

Import​

<script>
  import { useAppLifecycle } from '@classic-homes/native/svelte';
</script>
// React
import { useAppLifecycle } from '@classic-homes/native/react';

Basic Usage​

<script lang="ts">
  import { useAppLifecycle } from '@classic-homes/native/svelte';

  const lifecycle = useAppLifecycle({
    onStateChange: (state) => {
      if (!state.isActive) {
        console.log('App went to background - save state');
      }
    },
    onUrlOpen: (event) => {
      console.log('Deep link opened:', event.url);
    },
  });
</script>

<p>App is {lifecycle.isActive ? 'active' : 'in background'}</p>

API Reference​

Property Type Description
isActive boolean | null Whether app is currently in foreground
launchUrl string | null URL that launched the app (deep link)
error Error | null Current error if any
getState () => Promise<{ isActive: boolean }> Get current app state
getLaunchUrl () => Promise<string | null> Get URL that launched the app
exitApp () => Promise<void> Exit the app (Android only)
minimizeApp () => Promise<void> Minimize the app (Android only)

Accessibility​

  • Works with screen readers and assistive technologies
  • Provides loading and error states for accessible status updates
  • Keyboard accessible controls
  • Device - Get device information, battery status, and language
  • Network - Monitor network connectivity status in real-time
On this page