概述

#

一个新的 hidden 状态已添加到 AppLifecycleState 枚举中,用于表示应用程序不可见的情况。

背景

#

当调用 WidgetsBindingObserver.didChangeAppLifecycleState 时,AppLifecycleState 枚举用于指示应用程序处于哪个生命周期状态。

变更说明

#

新状态 AppLifecycleState.hidden 已添加到 dart:ui 包中的 AppLifecycleState 枚举中。

当所有应用程序视图对用户不再可见时,将进入 hidden 状态。在 Android 和 iOS 上,当状态机从非活跃状态 (inactive) 转换到暂停状态 (paused),或从暂停状态转换到非活跃状态时,会短暂进入此状态。它在进入暂停或非活跃状态时不会改变。在其他平台上,当应用程序不可见时,它将处于此状态。

迁移指南

#

如果代码中包含处理 AppLifecycleState 枚举所有情况的 switch 语句,则需要添加一个新的 case 来处理 AppLifecycleState.hidden 状态。

迁移前的代码

dart
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    case AppLifecycleState.resumed:
    case AppLifecycleState.inactive:
      // Do something when the app is visible...
      break;
    case AppLifecycleState.paused:
    case AppLifecycleState.detached:
      // Do something when the app is not visible...
      break;
  }
}

迁移后的代码

dart
void didChangeAppLifecycleState(AppLifecycleState state) {
  switch (state) {
    case AppLifecycleState.resumed:
    case AppLifecycleState.inactive:
      // Do something when the app is visible...
      break;
    case AppLifecycleState.hidden:  // <-- This is the new state.
    case AppLifecycleState.paused:
    case AppLifecycleState.detached:
      // Do something when the app is not visible...
      break;
  }
}

如果 switch 语句中已经存在 default: case,或者代码使用条件语句而非 switch 语句,则代码无需更改即可编译,但仍需评估默认 case 或条件语句,以决定是否也应处理 hidden 状态。

时间线

#

发布版本:3.11.0-16.0.pre
在稳定版中发布: 3.13.0

参考资料

#

相关 PR

  • PR 42418:添加 AppLifecycleState.hidden 枚举值