概述

#

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 列舉值