提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
在Android、iOS原生开发中都存在页面生命周期函如Android(Activity 生命周期,一个Activity相当于一个页面):
一、生命周期是什么?
一个页面创建到销毁的过程。
二、在flutter中监听页面生命周期的方法
1.RouteObserver
在MyApp定义RouteObserver
static final RouteObserver<ModalRoute<void>>routeObserver=RouteObserver<ModalRoute<void>();
2.navigatorObservers:
代码如下(示例):
MaterialApp(
navigatorObservers: [MyApp.routeObserver],
)
该处使用的url网络请求的数据。
3. with RouteAware
class _XXPageState extends State<XXPage> with RouteAware {
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
/// 路由订阅
MyApp.routeObserver.subscribe(this, ModalRoute.of(context)!);
}
void dispose() {
// TODO: implement dispose
/// 取消路由订阅
MyApp.routeObserver.unsubscribe(this);
super.dispose();
}
/// Called when the current route has been pushed.
void didPush() {
}
/// Called when the current route has been popped off.
void didPop() {
}
/// Called when the top route has been popped off, and the current route
/// shows up.
void didPopNext() {
}
/// Called when a new route has been pushed, and the current route is no
/// longer visible.
void didPushNext() {
}
}
总结
以上就是今天要讲的内容,在Android、ios中有直接可以复写的生命周期函数,但是在flutter中需要自己注册监听才能够使用其生命周期函数。
创作不易,请作者喝杯咖啡: