StatefulWidget 生命周期
createState
initState
didChangeDependencies
build
(didUpdateWidget)
deactivate
dispose
App 生命周期
使用 WidgetsBindingObserver 監(jiān)聽
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver
注冊(cè)監(jiān)聽/解除監(jiān)聽
initState: WidgetsBinding.instance.addObserver(this);
dispose: WidgetsBinding.instance.removeObserver(this);
//回調(diào)
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}