原创

06.Flutter生命周期


目录介绍

  • 01.首先看一个案例
  • 02.创建阶段生命周期
  • 03.App切后台,再切回来
  • 04.销毁阶段生命周期
  • 05.生命周期流程

好消息

  • 博客笔记大汇总【16年3月到至今】,包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bug汇总,当然也在工作之余收集了大量的面试题,长期更新维护并且修正,持续完善……开源的文件是markdown格式的!同时也开源了生活博客,从12年起,积累共计N篇[近100万字,陆续搬到网上],转载请注明出处,谢谢!
  • 链接地址:https://github.com/yangchong211/YCBlogs
  • 如果觉得好,可以star一下,谢谢!当然也欢迎提出建议,万事起于忽微,量变引起质变!

01.首先看一个案例

  • 代码如下所示
    class LifecycleAppPage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new _LifecycleAppPageState('构造函数');
      }
    }
    
    class _LifecycleAppPageState extends State<LifecycleAppPage>
        with WidgetsBindingObserver {
      String str;
    
      int count = 0;
    
      _LifecycleAppPageState(this.str);
    
      @override
      void initState() {
        LogUtils.showPrint(str);
        LogUtils.showPrint('initState');
        super.initState();
        WidgetsBinding.instance.addObserver(this);
      }
    
      @override
      void didChangeDependencies() {
        LogUtils.showPrint('didChangeDependencies');
        super.didChangeDependencies();
      }
    
      @override
      void didUpdateWidget(LifecycleAppPage oldWidget) {
        LogUtils.showPrint('didUpdateWidget');
        super.didUpdateWidget(oldWidget);
      }
    
      @override
      void deactivate() {
        LogUtils.showPrint('deactivate');
        super.deactivate();
      }
    
      @override
      void dispose() {
        LogUtils.showPrint('dispose');
        WidgetsBinding.instance.removeObserver(this);
        super.dispose();
      }
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        switch (state) {
          case AppLifecycleState.inactive:
            LogUtils.showPrint('AppLifecycleState.inactive');
            break;
          case AppLifecycleState.paused:
            LogUtils.showPrint('AppLifecycleState.paused');
            break;
          case AppLifecycleState.resumed:
            LogUtils.showPrint('AppLifecycleState.resumed');
            break;
          case AppLifecycleState.suspending:
            LogUtils.showPrint('AppLifecycleState.suspending');
            break;
        }
    
        super.didChangeAppLifecycleState(state);
      }
    
      @override
      Widget build(BuildContext context) {
        LogUtils.showPrint('build');
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('lifecycle 学习'),
            centerTitle: true,
          ),
          body: new OrientationBuilder(
            builder: (context, orientation) {
              return new Center(
                child: new Text(
                  '当前计数值:$count',
                  style: new TextStyle(
                      color: orientation == Orientation.portrait
                          ? Colors.blue
                          : Colors.red),
                ),
              );
            },
          ),
          floatingActionButton: new FloatingActionButton(
              child: new Text('click'),
              onPressed: () {
                count++;
                setState(() {});
              }),
        );
      }
    }
    
    class LifecyclePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new LifecycleAppPage(),
        );
      }
    }
    

02.创建阶段生命周期

  • 创建阶段生命周期如下所示
    2019-06-17 18:21:10.764 6370-6461/com.yczbj.ycflutter I/flutter: yc---------构造函数
    2019-06-17 18:21:10.764 6370-6461/com.yczbj.ycflutter I/flutter: yc---------initState
    2019-06-17 18:21:10.765 6370-6461/com.yczbj.ycflutter I/flutter: yc---------didChangeDependencies
    2019-06-17 18:21:10.769 6370-6461/com.yczbj.ycflutter I/flutter: yc---------build
    

03.App切后台,再切回来

  • App切到后台
    2019-06-17 18:21:59.554 6370-6461/com.yczbj.ycflutter I/flutter: yc---------AppLifecycleState.inactive
    2019-06-17 18:21:59.899 6370-6461/com.yczbj.ycflutter I/flutter: yc---------AppLifecycleState.paused
    
  • 再切回来
    2019-06-17 18:22:37.145 6370-6461/com.yczbj.ycflutter I/flutter: yc---------AppLifecycleState.inactive
    2019-06-17 18:22:37.153 6370-6461/com.yczbj.ycflutter I/flutter: yc---------AppLifecycleState.resumed
    

04.销毁阶段生命周期

  • 销毁阶段生命周期
    2019-06-17 18:25:27.106 6370-6461/com.yczbj.ycflutter I/flutter: yc---------deactivate
    2019-06-17 18:25:27.138 6370-6461/com.yczbj.ycflutter I/flutter: yc---------dispose
    

05.生命周期流程

  • 生命周期流程
    • image

其他介绍

01.关于博客汇总链接

02.关于我的博客

  • github:https://github.com/yangchong211
  • 知乎:https://www.zhihu.com/people/yczbj/activities
  • 简书:http://www.jianshu.com/u/b7b2c6ed9284
  • csdn:http://my.csdn.net/m0_37700275
  • 喜马拉雅听书:http://www.ximalaya.com/zhubo/71989305/
  • 开源中国:https://my.oschina.net/zbj1618/blog
  • 泡在网上的日子:http://www.jcodecraeer.com/member/content_list.php?channelid=1
  • 邮箱:yangchong211@163.com
  • 阿里云博客:https://yq.aliyun.com/users/article?spm=5176.100- 239.headeruserinfo.3.dT4bcV
  • segmentfault头条:https://segmentfault.com/u/xiangjianyu/articles
  • 掘金:https://juejin.im/user/5939433efe88c2006afa0c6e

评论

留言