添加 AppLifecycleState.hidden 的迁移指南
概述
#向 AppLifecycleState
枚举中添加了一个新的 hidden
状态,用于表示应用程序不可见的情况。
背景
#当调用 WidgetsBindingObserver.didChangeAppLifecycleState
时,AppLifecycleState
枚举用于指示应用程序处于哪种生命周期状态。
变更说明
#新的 AppLifecycleState.hidden
状态已添加到 dart:ui
包的 AppLifecycleState
枚举中。
当应用程序的所有视图都不再对用户可见时,应用程序将进入 hidden
状态。在 Android 和 iOS 上,当状态机从 inactive 迁移到 paused 或从 paused 迁移到 inactive 时,此状态会短暂进入。进入 paused 或 inactive 状态时不会改变。在其他平台上,当应用程序不可见时,它将处于此状态。
迁移指南
#如果代码中的 switch 语句处理了 AppLifecycleState
枚举的所有情况,则需要添加一个新的 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,或者代码使用了条件语句,那么代码将无需更改即可编译,但仍需要评估 default case 或条件语句以决定是否也应处理 hidden
状态。
时间线
#已于版本:3.11.0-16.0.pre 落地
在稳定版中发布: 3.13.0
参考资料
#相关 PR
- PR 42418:添加
AppLifecycleState.hidden
枚举值