AppLifecycleListener class

A listener that can be used to listen to changes in the application lifecycle.

To listen for requests for the application to exit, and to decide whether or not the application should exit when requested, create an AppLifecycleListener and set the onExitRequested callback.

To listen for changes in the application lifecycle state, define an onStateChange callback. See the AppLifecycleState enum for details on the various states.

The onStateChange callback is called for each state change, and the individual state transitions (onResume, onInactive, etc.) are also called if the state transition they represent occurs.

State changes will occur in accordance with the state machine described by this diagram:

Diagram of the application lifecycle defined by the AppLifecycleState enum

The initial state of the state machine is the AppLifecycleState.detached state, and the arrows describe valid state transitions. Transitions in blue are transitions that only happen on iOS and Android.

This example shows how an application can listen to changes in the application state.
link

To create a local project with this code sample, run:
flutter create --sample=widgets.AppLifecycleListener.1 mysample

This example shows how an application can optionally decide to abort a request for exiting instead of obeying the request.
link

To create a local project with this code sample, run:
flutter create --sample=widgets.AppLifecycleListener.2 mysample

See also:

Mixed in types

Constructors

AppLifecycleListener({WidgetsBinding? binding, VoidCallback? onResume, VoidCallback? onInactive, VoidCallback? onHide, VoidCallback? onShow, VoidCallback? onPause, VoidCallback? onRestart, VoidCallback? onDetach, AppExitRequestCallback? onExitRequested, ValueChanged<AppLifecycleState>? onStateChange})
Creates an AppLifecycleListener.

Properties

binding WidgetsBinding
The WidgetsBinding to listen to for application lifecycle events.
final
hashCode int
The hash code for this object.
no setterinherited
onDetach VoidCallback?
A callback that is called when an application has exited, and detached all host views from the engine.
final
onExitRequested AppExitRequestCallback?
A callback used to ask the application if it will allow exiting the application for cases where the exit is cancelable.
final
onHide VoidCallback?
A callback that is called when the application is hidden.
final
onInactive VoidCallback?
A callback that is called when the application loses input focus.
final
onPause VoidCallback?
A callback that is called when the application is paused.
final
onRestart VoidCallback?
A callback that is called when the application is resumed after being paused.
final
onResume VoidCallback?
A callback that is called when a view in the application gains input focus.
final
onShow VoidCallback?
A callback that is called when the application is shown.
final
onStateChange ValueChanged<AppLifecycleState>?
Called anytime the state changes, passing the new state.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
override
didChangeAccessibilityFeatures() → void
Called when the system changes the set of currently active accessibility features.
inherited
didChangeAppLifecycleState(AppLifecycleState state) → void
Called when the system puts the app in the background or returns the app to the foreground.
override
didChangeLocales(List<Locale>? locales) → void
Called when the system tells the app that the user's locale has changed. For example, if the user changes the system language settings.
inherited
didChangeMetrics() → void
Called when the application's dimensions change. For example, when a phone is rotated.
inherited
didChangePlatformBrightness() → void
Called when the platform brightness changes.
inherited
didChangeTextScaleFactor() → void
Called when the platform's text scale factor changes.
inherited
didChangeViewFocus(ViewFocusEvent event) → void
Called whenever the PlatformDispatcher receives a notification that the focus state on a view has changed.
inherited
didHaveMemoryPressure() → void
Called when the system is running low on memory.
inherited
didPopRoute() Future<bool>
Called when the system tells the app to pop the current route, such as after a system back button press or back gesture.
inherited
didPushRoute(String route) Future<bool>
Called when the host tells the application to push a new route onto the navigator.
inherited
didPushRouteInformation(RouteInformation routeInformation) Future<bool>
Called when the host tells the application to push a new RouteInformation and a restoration state onto the router.
inherited
didRequestAppExit() Future<AppExitResponse>
Called when a request is received from the system to exit the application.
override
dispose() → void
Call when the listener is no longer in use.
handleCancelBackGesture() → void
Called when a predictive back gesture is canceled, indicating that no navigation should occur.
inherited
handleCommitBackGesture() → void
Called when a predictive back gesture is finished successfully, indicating that the current route should be popped.
inherited
handleStartBackGesture(PredictiveBackEvent backEvent) bool
Called at the start of a predictive back gesture.
inherited
handleUpdateBackGestureProgress(PredictiveBackEvent backEvent) → void
Called when a predictive back gesture moves.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringShort() String
A brief description of this object, usually just the runtimeType and the hashCode.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited