Flutter 生命周期

2019-06-26  本文已影响0人  Passon_Fang

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
  }
}
上一篇下一篇

猜你喜欢

热点阅读