Skip to main content

Listening to App Lifecycle State Changes

Listening to App Lifecycle State Changes

Use this component to listen to application state changes. Whether the user switches to the background state (locks the device/goes into the background) or foreground state, if the user reopens the device.

To get started using it, you must use KmpLifecycle, and pass the action you want to run when the lifecycle state changes.

Foreground Subscription

To listen to when the app goes into the foreground, simply use this:


_3
KmpLifecycle.setAppLifecycleForeground {
_3
// my action to run when the user returns to the app (foreground state)
_3
}

Background/Device Lock Subscription

To listen to when the app goes into the background state or if the user locks their device:


_3
KmpLifecycle.setAppLifecycleBackground {
_3
// my action to run when the user returns to the app (background state)
_3
}

Cleaning up memory

When subscribing you must make sure to deallocate the subscriptions otherwise you could cause a memory leak. These are global subscriptions (relative to the application layer), so if you don't unsubscribe and for example if you listen to a background state change on one of your screens, once the screen has been disposed of by the garbage collector or the user has closed the page, that action will still remain active. So it's crucial to cleanup your subscriptions after being done with them. This is usually done when the activity/fragment/viewcontroller are closed/disappeared.

You can call this function to reset it:


_1
KmpLifecycle.resetAppLifecycleActions()

Android Setup

None Required

iOS Setup

None required