一般在main阶段就可以添加一个全局观察者
void main() {
///添加全局生命周期监听类
PageVisibilityBinding.instance.addGlobalObserver(AppLifecycleObserver());
runApp(MyApp());
}
AppLifecycleObserver
的具体实现如下
///全局生命周期监听示例
class AppLifecycleObserver with GlobalPageVisibilityObserver {
@override
void onBackground(Route route) {
super.onBackground(route);
print("AppLifecycleObserver - onBackground");
}
@override
void onForeground(Route route) {
super.onForeground(route);
print("AppLifecycleObserver - onForground");
}
@override
void onPagePush(Route route) {
super.onPagePush(route);
print("AppLifecycleObserver - onPagePush");
}
@override
void onPagePop(Route route) {
super.onPagePop(route);
print("AppLifecycleObserver - onPagePop");
}
@override
void onPageHide(Route route) {
super.onPageHide(route);
print("AppLifecycleObserver - onPageHide");
}
@override
void onPageShow(Route route) {
super.onPageShow(route);
print("AppLifecycleObserver - onPageShow");
}
}
///单个生命周期示例
class LifecycleTestPage extends StatefulWidget {
const LifecycleTestPage({Key key}) : super(key: key);
@override
_LifecycleTestPageState createState() => _LifecycleTestPageState();
}
class _LifecycleTestPageState extends State<LifecycleTestPage>
with PageVisibilityObserver {
@override
void onBackground() {
super.onBackground();
print("LifecycleTestPage - onBackground");
}
@override
void onForeground() {
super.onForeground();
print("LifecycleTestPage - onForeground");
}
@override
void onPageHide() {
super.onPageHide();
print("LifecycleTestPage - onPageHide");
}
@override
void onPageShow() {
super.onPageShow();
print("LifecycleTestPage - onPageShow");
}
@override
void initState() {
super.initState();
///请在didChangeDependencies中注册而不是initState中
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
///注册监听器
PageVisibilityBinding.instance.addObserver(this, ModalRoute.of(context));
}
@override
void dispose() {
///移除监听器
PageVisibilityBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return ...;
}
}
-
在页面层级,没有
push
事件和pop
事件,初始化逻辑请直接写在initState
,卸载逻辑请写在dispose
中即可 -
onPageShow
对标AndroidonResume
,iOSviewDidAppear
-
onPageHide
对标AndroidonStop
,iOSviewDidDisappear