Flutter生命週期的獲取

在Android開發中,系統對Activity、Fragment的生命週期有着很是明顯且較於區分的定義,可是在flutter中,因爲flutter的生命週期依附在activity或fragment,它的生命週期就不一樣以往了,下面就展現如下flutter生命週期的理解。android

flutter 生命週期相關函數的調用過程

首先,先上一張圖,這張圖很簡單明瞭的闡釋了一個頁面啓動所要執行的widget方法流程: ios

下面解釋一下各個方法的做用:app

initState

在生命週期中只調用一次,此時沒法獲取widget對象,能夠作一些初始化操做。ide

didChangeDependencies

當State對象的依賴發生變化時會被調用;例如:在以前build() 中包含了一個InheritedWidget,而後在以後的build() 中InheritedWidget發生了變化,那麼此時InheritedWidget的子widget的didChangeDependencies()回調都會被調用。InheritedWidget這個widget能夠由父控件向子控件共享數據,案例能夠參考 scoped_model開源庫。函數

didUpdateWidget

widget狀態改變的時候調用學習

deactivate

相似於Activity的onResume和onStop,兩種狀態都會調用ui

dispose

相似於Android的onDestroythis

上面的介紹都比較簡單,下面則介紹如下,如何去獲取app的生命週期code

flutter app的生命週期

flutter提供了一個枚舉類來表明了app各個生命週期的狀態:server

enum AppLifecycleState {
  /// The application is visible and responding to user input.
  resumed,

  /// The application is in an inactive state and is not receiving user input.
  ///
  /// On iOS, this state corresponds to an app or the Flutter host view running
  /// in the foreground inactive state. Apps transition to this state when in
  /// a phone call, responding to a TouchID request, when entering the app
  /// switcher or the control center, or when the UIViewController hosting the
  /// Flutter app is transitioning.
  ///
  /// On Android, this corresponds to an app or the Flutter host view running
  /// in the foreground inactive state.  Apps transition to this state when
  /// another activity is focused, such as a split-screen app, a phone call,
  /// a picture-in-picture app, a system dialog, or another window.
  ///
  /// Apps in this state should assume that they may be [paused] at any time.
  inactive,

  /// The application is not currently visible to the user, not responding to
  /// user input, and running in the background.
  ///
  /// When the application is in this state, the engine will not call the
  /// [Window.onBeginFrame] and [Window.onDrawFrame] callbacks.
  ///
  /// Android apps in this state should assume that they may enter the
  /// [suspending] state at any time.
  paused,

  /// The application will be suspended momentarily.
  ///
  /// When the application is in this state, the engine will not call the
  /// [Window.onBeginFrame] and [Window.onDrawFrame] callbacks.
  ///
  /// On iOS, this state is currently unused.
  suspending,
}

resumed

應用程序對用戶可見的時候輸出

inactive

界面處於不可點擊狀態,可是可見時候的回調,相似於Android的onpause

paused

app處於不可見的時候,相似於Android的onStop

suspending

ios中這個屬性無效,android中表明處於後臺

獲取方法

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  AppLifecycleState _lastLifecycleState;

  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
  }

  ...
}

注意:第一次進入的時候並不會執行didChangeAppLifecycleState方法。 獲取app的生命週期方法很簡單,可是注意這並非當前widget的生命週期,那咱們若是獲取當前頁面的生命週期呢。

獲取flutter頁面的生命週期

當flutter頁面跳轉切入後臺,flutter並無清楚的給咱們展現flutter頁面的各個生命週期狀態。若是咱們想要獲取某個widget頁面的狀態,好比可見不可見那該如何操做呢?

flutter頁面的onDestroy

這個比較簡單,重寫State的dispose,這個方法便可理解爲頁面的onDestroy操做。

flutter頁面的onStop、onResume

上面介紹了deactivate相似於activity的onResume、onStop那麼咱們能夠利用這個函數來本身標誌一下生命週期。 由於deactivate這個方法第一次是不執行的,所以咱們能夠定義一個默認值isVisible爲true來表明是否可見。

class MyState extends State<StatefulWidget>{

  bool isVisible = true;

  @override
  void deactivate() {
    isVisible = !isVisible;
    if(isVisible){
      //onResume
    }else {
      //onStop
    }
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return null;
  }

}

這時候咱們就能夠經過isVisible來判斷當前頁面是否可見了,以此來作一些操做。 今年金九銀十我花一個月的時間收錄整理了一套知識體系,若是有想法深刻的系統化的去學習的,能夠點擊傳送門,我會把我收錄整理的資料都送給你們,幫助你們更快的進階。

相關文章
相關標籤/搜索