颤抖didChangeAppLifecycleState永远不会运行

时间:2020-08-28 22:21:20

标签: flutter dart

我实现了WidgetsBindingObserver,但是该应用从未发送到后台,因此它无法识别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中进行下一步

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

返回到应用程序后,我可以在屏幕上看到我的窗口小部件,该应用程序不会重新启动,但是如果没有执行print('\n\ndidChangeAppLifecycleState');_mymethod();,则控制台上将不会显示任何打印内容< / p>

1 个答案:

答案 0 :(得分:2)

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

# models.py

class ListModel(models.Model):
    . . .
    student_id = models.Field()


# views.py

    if request.POST.get('expel_all_students'):
        for student_id in table:
            expelled_students_id = str(request.POST.get('expelled_student'))
            expelled_student_instance = ListModel.objects.get(id = expelled_students_id)
            student_id.rm(expelled_student_instance)

# templates.py
<button type="submit" value='expel_all_students' name="expel_all_students"> Expel all students </button>

<table>
    . . .