In Android development, the system has a very clear and distinct definition of Activity and Fragment lifecycles, but with flutter, because the life cycle of the flutter is attached to the Activity or Fragment, the life cycle is different. Below is an understanding of the life cycle of flutter.

The process of calling the flutter life-cycle related functions

First, let’s start with the following diagram, which illustrates the process of executing the widget method for a page launch in a very simple way:

Here’s what each method does:

initState

This is called only once in the lifecycle, when the Widget object cannot be retrieved, and some initialization can be done.

didChangeDependencies

Called when the dependencies of the State object change; Such as: If you included an InheritedWidget in the previous build(), and then you changed the InheritedWidget in the subsequent build(), Then the didChangeDependencies() callback for the InheritedWidget’s children is called. InheritedWidget is a widget that can share data from a parent control to a child control. See the Scoped_Model open source library for examples.

didUpdateWidget

Called when the state of the widget changes

deactivate

OnResume and onStop, similar to an Activity, are called in both states

dispose

Similar to Android’s onDestroy

The above introduction is relatively simple, the following introduction, how to get app life cycle

Life cycle of the Flutter App

Flutter provides an enumeration class to represent the state of the app’s lifecycle:

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,
}
Copy the code

resumed

Output when the application is visible to the user

inactive

The interface is not clickable, but the callback is visible, similar to Android’s onPause

paused

When the app is not visible, it is similar to Android’s onStop

suspending

In ios this property is invalid, in Android it means in the background

Access method

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); }... }Copy the code

Note: entering for the first time will not perform didChangeAppLifecycleState method. Getting the life cycle of the app is easy, but notice that this is not the life cycle of the current widget, so what if we get the life cycle of the current page?

Get the life cycle of the Flutter page

As the Flutter page jumps into the background, the flutter does not give us a clear picture of the various life cycle states of the Flutter page. What if we want to get the state of a widget page, such as whether it is visible or not?

OnDestroy on the Flutter page

This is simple, overriding the State dispose method, which can be understood as the onDestroy operation on the page.

OnStop and onResume on the Flutter page

Deactivate is similar to the activity’s onResume and onStop functions, so we can use this function to mark the lifecycle. Since the deactivate method does not execute the first time, we can define a default value of isVisible to true to indicate whether it isVisible.

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;
  }

}
Copy the code

At this point, we can use isVisible to determine whether the current page isVisible, so we can do some operations.