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) {
super.didChangeAppLifecycleState(state);
// print(state);
if (_history.isEmpty) return;
switch (state) {
case AppLifecycleState.resumed: // active
// Top route trigger active
//
// 最上面的route触发active
_sendEventsToLastRoute(lifecycleEventsVisibleAndActive);
if (_history.last.route is! PageRoute) {
// Previous PageRoute trigger visible
//
// 前一个PageRoute触发visible
_sendEventsToLastPageRoute([LifecycleEvent.visible]);
}
break;
case AppLifecycleState.inactive: // inactive
// Top route trigger inactive
//
// 最上面的route触发inactive
_sendEventsToLastRoute([LifecycleEvent.inactive]);
break;
case AppLifecycleState.paused: // invisible
// Top route trigger invisible
//
// 最上面的route触发invisible
_sendEventsToLastRoute([LifecycleEvent.invisible]);
if (_history.last.route is! PageRoute) {
// Previous PageRoute trigger invisible
//
// 前一个PageRoute触发invisible
_sendEventsToLastPageRoute([LifecycleEvent.invisible]);
}
break;
case AppLifecycleState.detached:
break;
}
}