Flutter生命周期的获取
在Android开发中,系统对Activity、Fragment的生命周期有着非常显著且较于区分的定义,但是在flutter中,因为flutter的生命周期依附在activity或者fragment,它的生命周期就不同以往了,下面就展现以下flutter生命周期的了解。
flutter 生命周期相关函数的调用过程
首先,先上一张图,这张图很简单明了的阐释了一个页面启动所要执行的widget方法流程:

下面解释一下各个方法的作用:
initState
在生命周期中只调用一次,此时无法获取widget对象,可以做少量初始化操作。
didChangeDependencies
当State对象的依赖发生变化时会被调用;例如:在之前build() 中包含了一个InheritedWidget,而后在之后的build() 中InheritedWidget发生了变化,那么此时InheritedWidget的子widget的didChangeDependencies()回调都会被调用。InheritedWidget这个widget可以由父控件向子控件共享数据,案例可以参考 scoped_model开源库。
didUpdateWidget
widget状态改变的时候调用
deactivate
相似于Activity的onResume和onStop,两种状态都会调用
dispose
相似于Android的onDestroy
上面的详情都比较简单,下面则详情以下,如何去获取app的生命周期
flutter app的生命周期
flutter提供了一个枚举类来代表了app各个生命周期的状态:
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来判断当前页面能否可见了,以此来做少量操作。
今年金九银十我花一个月的时间收录整理了一套知识体系,假如有想法深入的系统化的去学习的,可以点击传送门,我会把我收录整理的资料都送给大家,帮助大家更快的进阶。

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Flutter生命周期的获取