新增 AppLifecycleState.hidden 的遷移指南
摘要
#在 AppLifecycleState
列舉中新增了一個新的 hidden
狀態,表示應用程式不可見時。
背景
#當呼叫 WidgetsBindingObserver.didChangeAppLifecycleState
時,AppLifecycleState
列舉用於指示應用程式處於哪個生命週期狀態。
變更說明
#新的狀態 AppLifecycleState.hidden
已新增至 dart:ui
套件中的 AppLifecycleState
列舉。
當應用程式的所有檢視畫面都不再對使用者可見時,會進入 hidden
狀態。在 Android 和 iOS 上,當狀態機從非活動狀態轉換為暫停狀態,或從暫停狀態轉換為非活動狀態時,會短暫進入此狀態。當進入暫停或非活動狀態時,它不會改變。在其他平台上,當應用程式不可見時,它會處於此狀態。
遷移指南
#如果程式碼具有處理 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,或者程式碼改用條件判斷式,則程式碼將會在不進行變更的情況下編譯,但仍需要評估 default case 或條件判斷式,以決定是否也應該處理 hidden
狀態。
時間軸
#已在版本中發布:3.11.0-16.0.pre
穩定版本:3.13.0
參考資料
#相關 PR
- PR 42418:新增
AppLifecycleState.hidden
列舉值
除非另有說明,否則本網站上的文件反映了 Flutter 的最新穩定版本。頁面最後更新於 2024-04-04。 檢視原始碼 或回報問題。