StatefulWidget 生命周期
createState
initState
didChangeDependencies
build
(didUpdateWidget)
deactivate
dispose
App 生命周期
使用 WidgetsBindingObserver 监听
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver
注册监听/解除监听
initState: WidgetsBinding.instance.addObserver(this);
dispose: WidgetsBinding.instance.removeObserver(this);
//回调
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}
网友评论