listenToApplicationLifecycleChangesIfSupported method

  1. @visibleForTesting
void listenToApplicationLifecycleChangesIfSupported()

Enables this FocusManager to listen to changes of the application lifecycle if it does not already have an application lifecycle listener active, and the app isn't running on a native mobile platform.

Typically, the application lifecycle listener for this FocusManager is setup at construction, but sometimes it is necessary to manually initialize it when the FocusManager does not have the relevant platform context in defaultTargetPlatform at the time of construction. This can happen in a test environment where the BuildOwner which initializes its own FocusManager, may not have the accurate platform context during its initialization. In this case it is necessary for the test framework to call this method after it has set up the test variant for a given test, so the FocusManager can accurately listen to application lifecycle changes, if supported.

Implementation

@visibleForTesting
void listenToApplicationLifecycleChangesIfSupported() {
  if (_appLifecycleListener == null && _respondToWindowFocus) {
    _appLifecycleListener = _AppLifecycleListener(_appLifecycleChange);
    WidgetsBinding.instance.addObserver(_appLifecycleListener!);
  }
}