颤抖didChangeAppLifecycleState永远不会运行 [英] flutter didChangeAppLifecycleState never runs

查看:82
本文介绍了颤抖didChangeAppLifecycleState永远不会运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了 WidgetsBindingObserver ,但是该应用程序从未发送到后台,因此它无法识别 AppLifecycleState.resumed

I implemented the WidgetsBindingObserver, but the app is NEVER sent to the background so it doesn't recognize the AppLifecycleState.resumed

这是当前的实现方式


  @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;
    }
  }

模拟该过程,我将在android中进行下一步

to simulate the process i do the next in android

  1. 以--release
  2. 运行项目
  3. 使用 WidgetsBindingObserver
  4. 打开窗口小部件
  5. 打开其他应用(例如Chrome浏览器或手机设置)
  6. 返回到应用程序

返回到应用程序后,我可以在屏幕上看到我的窗口小部件,该应用程序不会重新启动,但控制台上不会显示任何打印件,而不会出现 print('\ n \ ndidChangeAppLifecycleState'); _mymethod(); 从未执行

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'); and _mymethod(); is never executed

推荐答案

WidgetsBindingObserver mixin不仅需要实现接口,还需要做更多的工作.您还需要将以下内容添加到窗口小部件状态类中:

The WidgetsBindingObserver mixin requires a bit more work than merely implementing the interface. 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();
}

这篇关于颤抖didChangeAppLifecycleState永远不会运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆