Record the callback when Flutter monitors the APP life cycle changes

Directly upload the code, copy it and use it directly for monitoring

class HomeMain extends StatefulWidget {
  @override
  _HomeMain createState() => _HomeMain();
}

class _HomeMain extends State<HomeMain> with WidgetsBindingObserver {
@override
void initState() {
  super.initState();
  WidgetsBinding.instance!.addObserver(this); //添加观察者
}
  /// Callback when the life cycle changes 
  /// resumed: the application is visible and can respond to user operations 
  /// inactive: the user is visible, but cannot respond to user operations 
  /// paused: has been paused, the user is not visible and cannot be operated 
  /// /suspending: The application is suspended, this state IOS will never call back 
  @override 
  void didChangeAppLifecycleState(AppLifecycleState state) { 
    // TODO: implement didChangeAppLifecycleState 
    super.didChangeAppLifecycleState(state); 
    switch(state){ 
      case AppLifecycleState.resumed: 
// TODO : Switch to the foreground from the background, the interface is visible. 
        LogUtils.v('resumed'); 
        break; 
      case AppLifecycleState.inactive: 
        // TODO: visible to the user, but cannot respond to user operations 
        debugPrint('inactive'); 
        LogUtils.v('inactive ');
       

Guess you like

Origin blog.csdn.net/wxx314165038/article/details/120956657