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);
switch (state) {
case AppLifecycleState.inactive:
// 应用程序处于闲置状态并且没有收到用户的输入事件。
//注意这个状态,在切换到后台时候会触发,所以流程应该是先冻结窗口,然后停止UI
if (Get.currentRoute.contains(widget.runtimeType.toString())) {
onBackHome();
}
break;
case AppLifecycleState.paused:
// 应用程序处于不可见状态
if (Get.currentRoute.contains(widget.runtimeType.toString())) {
onPause();
}
break;
case AppLifecycleState.resumed:
// 进入应用时候不会触发该状态
// 应用程序处于可见状态,并且可以响应用户的输入事件。它相当于 Android 中Activity的onResume。
if (Get.currentRoute.contains(widget.runtimeType.toString())) {
onResume();
}
break;
case AppLifecycleState.detached:
//当前页面即将退出
if (Get.currentRoute.contains(widget.runtimeType.toString())) {
onDetach();
}
break;
}
}