在Android开发中,系统对Activity、Fragment的生命周期有着很是明显且较于区分的定义,可是在flutter中,因为flutter的生命周期依附在activity或fragment,它的生命周期就不一样以往了,下面就展现如下flutter生命周期的理解。android
首先,先上一张图,这张图很简单明了的阐释了一个页面启动所要执行的widget方法流程: ios
下面解释一下各个方法的做用:app
在生命周期中只调用一次,此时没法获取widget对象,能够作一些初始化操做。ide
当State对象的依赖发生变化时会被调用;例如:在以前build() 中包含了一个InheritedWidget,而后在以后的build() 中InheritedWidget发生了变化,那么此时InheritedWidget的子widget的didChangeDependencies()回调都会被调用。InheritedWidget这个widget能够由父控件向子控件共享数据,案例能够参考 scoped_model开源库。函数
widget状态改变的时候调用学习
相似于Activity的onResume和onStop,两种状态都会调用ui
相似于Android的onDestroythis
上面的介绍都比较简单,下面则介绍如下,如何去获取app的生命周期code
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, }
应用程序对用户可见的时候输出
界面处于不可点击状态,可是可见时候的回调,相似于Android的onpause
app处于不可见的时候,相似于Android的onStop
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页面的各个生命周期状态。若是咱们想要获取某个widget页面的状态,好比可见不可见那该如何操做呢?
这个比较简单,重写State的dispose,这个方法便可理解为页面的onDestroy操做。
上面介绍了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来判断当前页面是否可见了,以此来作一些操做。 今年金九银十我花一个月的时间收录整理了一套知识体系,若是有想法深刻的系统化的去学习的,能够点击传送门,我会把我收录整理的资料都送给你们,帮助你们更快的进阶。