简体   繁体   English

flutter didChangeAppLifecycleState 从不运行

[英]flutter didChangeAppLifecycleState never runs

I implemented the WidgetsBindingObserver , but the app is NEVER sent to the background so it doesn't recognize the AppLifecycleState.resumed我实现了WidgetsBindingObserver ,但该应用程序从未发送到后台,因此它无法识别AppLifecycleState.resumed

this is the current implementation这是当前的实现


  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    print('\n\ndidChangeAppLifecycleState');
    switch (state) {
      case AppLifecycleState.resumed:
        print('\n\nresumed');
        _mymethod();
        break;
      case AppLifecycleState.inactive:
        print('\n\ninactive');
        break;
      case AppLifecycleState.paused:
        print('\n\npaused');
        break;
      case AppLifecycleState.detached:
        print('\n\ndetached');
        break;
    }
  }

to simulate the process i do the next in android模拟我在android中做下一个的过程

  1. run the project as --release将项目作为 --release 运行
  2. open the widget with the WidgetsBindingObserver使用WidgetsBindingObserver打开小部件
  3. open another app (like chrome or phone settings)打开另一个应用程序(如 Chrome 或手机设置)
  4. return to the app返回应用程序

when returning to the app i can see my widget on screen, the app doesn't restart, but NONE of the prints appears on the console not event the print('\\n\\ndidChangeAppLifecycleState');返回应用程序时,我可以在屏幕上看到我的小部件,该应用程序不会重新启动,但控制台上没有任何打印显示,而不是事件print('\\n\\ndidChangeAppLifecycleState'); and _mymethod();_mymethod(); is never executed永远不会被执行

The WidgetsBindingObserver mixin requires a bit more work than merely implementing the interface. WidgetsBindingObserver mixin 比仅仅实现接口需要更多的工作。 You also need to add the following to your widget state class:您还需要将以下内容添加到您的小部件状态类:

@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
  WidgetsBinding.instance.removeObserver(this);
  super.dispose();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM