didChangeAppLifecycleState method
Called when the system puts the app in the background or returns the app to the foreground.
An example of implementing this method is provided in the class-level documentation for the WidgetsBindingObserver class.
This method exposes notifications from SystemChannels.lifecycle.
See also:
- AppLifecycleListener, an alternative API for responding to application lifecycle changes.
Implementation
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
final hub = _hub;
if (!_isActive || hub == null) return;
// Reserve event metadata immediately at the start of lifecycle change
final reservation = hub.reserveEventMetadata();
final now = DateTime.now();
final previous = _currentState;
final sinceLast = _lastStateChange != null
? now.difference(_lastStateChange!).inMilliseconds
: null;
_currentState = state;
_lastStateChange = now;
_isInBackground = _mapIsBackground(state);
final stage = _mapStage(state);
hub.captureEvent(
AppLifecycleEventBase(
stage: stage,
viewName: null,
timeSinceLastChangeMs: sinceLast,
previousState: previous?.name,
),
reservation);
}