didChangeAppLifecycleState method

  1. @override
void didChangeAppLifecycleState(
  1. AppLifecycleState state
)
override

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:

Implementation

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  // The other SDKs send three states; Flutter's hidden, paused and detached are all the app out of sight.
  final name = switch (state) {
    AppLifecycleState.resumed => 'active',
    AppLifecycleState.inactive => 'inactive',
    AppLifecycleState.hidden || AppLifecycleState.paused || AppLifecycleState.detached => 'background',
  };
  if (name == _state) return;
  _state = name;

  final size = WidgetsBinding.instance.platformDispatcher.views.firstOrNull?.physicalSize;
  final orientation = size == null ? 'unknown' : size.height >= size.width ? 'portrait' : 'landscape';
  LogManager().push(AppEvent('change', name, orientation));
}