handleLifecycleChange method

void handleLifecycleChange(
  1. AppLifecycleState state
)

Feeds a lifecycle change into the lock logic. Called automatically after start; public for apps that track the lifecycle themselves.

Implementation

void handleLifecycleChange(AppLifecycleState state) {
  if (_disposed) return;
  // Lifecycle noise caused by the system authentication prompt.
  if (_unlocking != null) return;

  // `hidden` only exists on Flutter 3.13+, so compare by name.
  final backgrounded =
      state == AppLifecycleState.paused || state.name == 'hidden';

  if (backgrounded) {
    _backgroundedAt ??= _now();
    // Lock right away so the content is already covered in the app
    // switcher snapshot and on return.
    if (gracePeriod == Duration.zero) _setLocked(true);
  } else if (state == AppLifecycleState.resumed) {
    final since = _backgroundedAt;
    _backgroundedAt = null;
    if (since == null) return;
    final away = _now().difference(since);
    // A negative duration means the clock was moved back: lock.
    if (away.isNegative || away >= gracePeriod) _setLocked(true);
  }
}