• activity与fragment交叉生命周期


    与https://blog.csdn.net/u010513497/article/details/50961042结果一致。

    与https://blog.csdn.net/shuijianbaozi/article/details/52994525不一致,可能是设置fragment的方式不一样。

    待解决的疑问:什么情况下fragment调用onInflate()

    Fragment生命周期

    ①Activity加载Fragment的时候,依次调用下面的方法: onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart ->onResume
    
    ②当我们弄出一个悬浮的对话框风格的Activity,或者其他,就是让Fragment所在的Activity可见,但不获得焦点 onPause
    
    ③当对话框关闭,Activity又获得了焦点: onResume
    
    ④当我们替换Fragment,并调用addToBackStack()将他添加到Back栈中 onPause -> onStop -> onDestoryView !!注意,此时的Fragment还没有被销毁哦!!!
    
    ⑤当我们按下键盘的回退键,Fragment会再次显示出来: onCreateView -> onActivityCreated -> onStart -> onResume
    
    ⑥如果我们替换后,在事务commit之前没有调用addToBackStack()方法将 Fragment添加到back栈中的话;又或者退出了Activity的话,那么Fragment将会被完全结束, Fragment会进入销毁状态 onPause -> onStop -> onDestoryView -> onDestory -> onDetach

    只有一个activity

    有可以通过底部的三个tab切换不同的fragment

    第一次进入APP
    2022-02-16 22:10:07.603 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onCreate: 2022-02-16 22:10:07.726 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onAttach: 2022-02-16 22:10:07.726 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreate: 2022-02-16 22:10:07.726 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreateView: 2022-02-16 22:10:07.729 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onActivityCreated: 2022-02-16 22:10:07.729 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onViewStateRestored: 2022-02-16 22:10:07.732 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStart: 2022-02-16 22:10:07.733 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onStart 2022-02-16 22:10:07.737 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onResume 2022-02-16 22:10:07.738 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onResume: home 2022-02-16 22:10:32.210 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onPause: 2022-02-16 22:10:32.210 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onPause 2022-02-16 22:10:32.248 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStop: 2022-02-16 22:10:32.248 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onStop: 2022-02-16 22:10:32.255 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onSaveInstanceState: 在stop后调用 重新点开(此时程序未退出) 2022-02-16 22:10:48.743 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onRestart: 2022-02-16 22:10:48.745 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStart: 2022-02-16 22:10:48.746 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onStart 2022-02-16 22:10:48.747 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onResume 2022-02-16 22:10:48.748 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onResume: 切换第二个fragment 2022-02-16 22:11:31.134 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onAttach: 2022-02-16 22:11:31.134 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onCreate: 2022-02-16 22:11:31.139 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onActivityCreated: 2022-02-16 22:11:31.140 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onViewStateRestored: 2022-02-16 22:11:31.140 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onStart: 2022-02-16 22:11:31.140 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onResume: 2022-02-16 22:11:31.142 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onPause: 2022-02-16 22:11:31.142 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStop: 2022-02-16 22:11:31.283 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDestroyView: 点击第一个fragment导航栏对应的TAb,创建了新的homefragment。Tag1="从第二个fragment点击第一个fragment对应的TAB,此时创建了新的homefragment"
    tab2022-02-16 22:11:56.795 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onAttach: 2022-02-16 22:11:56.795 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreate: 2022-02-16 22:11:56.796 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreateView: 2022-02-16 22:11:56.799 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onActivityCreated: 2022-02-16 22:11:56.799 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onViewStateRestored: 2022-02-16 22:11:56.799 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStart: 2022-02-16 22:11:56.800 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onResume: 2022-02-16 22:11:56.801 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDestroy: 这应该是上一个homefragment 2022-02-16 22:11:56.801 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDetach: 这应该是上一个homefragment 2022-02-16 22:11:56.801 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onPause: 2022-02-16 22:11:56.801 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onStop: 2022-02-16 22:11:56.953 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDestroyView: 2022-02-16 22:11:56.953 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDestroy: 2022-02-16 22:11:56.953 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDetach:


    点击返回按钮:退出了APP

    2022-02-16 22:15:13.693 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onPause:
    2022-02-16 22:15:13.693 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onPause
    2022-02-16 22:15:14.204 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStop:
    2022-02-16 22:15:14.205 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onStop:
    2022-02-16 22:15:14.218 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDestroyView:
    2022-02-16 22:15:14.219 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDestroy:
    2022-02-16 22:15:14.219 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDetach:
    2022-02-16 22:15:14.220 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onDestroy:

    再次进入app(与第一次进入app相同)
    2022-02-16 22:16:35.872 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onCreate:
    2022-02-16 22:16:35.920 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onAttach:
    2022-02-16 22:16:35.921 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreate:
    2022-02-16 22:16:35.921 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreateView:
    2022-02-16 22:16:35.923 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onActivityCreated:
    2022-02-16 22:16:35.923 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onViewStateRestored:
    2022-02-16 22:16:35.924 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStart:
    2022-02-16 22:16:35.924 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onStart
    2022-02-16 22:16:35.925 26756-26756/com.example.bottomnavigationactivityapp I/MyMainActivity: onResume
    2022-02-16 22:16:35.926 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onResume:


    点击第二个fragment

    2022-02-16 22:17:08.680 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onAttach:
    2022-02-16 22:17:08.680 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onCreate:
    2022-02-16 22:17:08.684 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onActivityCreated:
    2022-02-16 22:17:08.684 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onViewStateRestored:
    2022-02-16 22:17:08.684 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onStart:
    2022-02-16 22:17:08.685 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onResume:
    2022-02-16 22:17:08.686 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onPause:
    2022-02-16 22:17:08.686 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStop:
    2022-02-16 22:17:08.835 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onDestroyView:


    点击返回,返回了先前的fragment。注意与这儿与tag1出的操作不同,没有生成新的homefragment。
    2022-02-16 22:18:01.121 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onCreateView:
    2022-02-16 22:18:01.125 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onActivityCreated:
    2022-02-16 22:18:01.126 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onViewStateRestored:
    2022-02-16 22:18:01.126 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onStart:
    2022-02-16 22:18:01.126 26756-26756/com.example.bottomnavigationactivityapp I/My------Home------Fragment: onResume:
    2022-02-16 22:18:01.127 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onPause:
    2022-02-16 22:18:01.127 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onStop:
    2022-02-16 22:18:01.307 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDestroyView:
    2022-02-16 22:18:01.308 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDestroy:
    2022-02-16 22:18:01.308 26756-26756/com.example.bottomnavigationactivityapp I/My------Dashboard------Fragment: onDetach:

    Activity生命周期

    异常生命周期下数据的onSaveInstanceState的使用

    2017.11.08 23:25:10字数 489阅读 449
        onSaveIntanceState方法和onRestoreInstanceState方法是成对出现的,在正常的生命周期下是不会被调用的,除非在异常生命周期下会被调用用来保存和恢复数据。
        异常生命生命周期下常见有两种,分贝为资源相关的系统配置发生改变时(如横竖屏切换时显示不同的图片资源)和系统资源不足回收低优先级的Activity导致的Activity重建。
        实现过程为当Activity发生异常时,Activity调用onSaveInstanceState方法中使用Bundle存储数据,然后Activity会委托Window去保存数据,接着window委托父类继续保存数据,顶层为ViewGroup,到达顶层后然后再去通知各个子View去保存数据。onSaveInstanceState调用发生在onStop方法之前,和onPause方法没有前后之分,执行onDestory方法销毁当前的Activity,然后创建新的Activity,使用onCreate和onRestoreIntanceState方法去恢复数据。这两个都可以进行数据恢复,但是区别在于onRestoreIntanceState一旦调用的话其中的Bundle参数一定会有值,而onCreate不一定,在正常生命周期下的Bundle参数就是为空,此使用onCreate方法就进行判空处理,官方建议使用onRestoreIntanceState进行处理数据恢复。
        当系统资源不足时,系统会按照低优先级回收Activity,这种情况下也会触发onSaveInstanceState方法的进行数据保存以及onRestoreIntanceState方法进行数据恢复。回收优先级分为三种后台、可见但非前台等级。
        总而言之,系统只有在Activity异常终止的情况下调用onSaveInstanceState和onRestoreIntanceState进行数据保存与恢复。其他情况下不会调用。

    在fragment的构造方法获取不到相应activity的taskId,可能是这个时候activity还没有创建好。并没有对应的taskId

  • 相关阅读:
    C++ PRIMER 学习笔记 第五章
    C++ PRIMER 学习笔记 第八章
    C++ PRIMER 学习笔记 第九章
    C++ PRIMER 学习笔记 第七章
    Qt5之QML语言与Qt Quick
    C++ PRIMER 学习笔记 第三章
    C++ PRIMER 学习笔记 第六章
    C++ Primer 学习笔记 第二章
    C++ PRIMER 学习笔记 第四章
    Sublime Text3配置Qt
  • 原文地址:https://www.cnblogs.com/sunupo/p/15902604.html
Copyright © 2020-2023  润新知