Flutter 生命周期

StatefulWidget 生命周期

createState
initState
didChangeDependencies
build
(didUpdateWidget)

deactivate
dispose

App 生命周期

使用 WidgetsBindingObserver 监听

class _MyScreenState extends State 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
  }
}

你可能感兴趣的:(Flutter 生命周期)