Activity与Fragment同时运行时生命周期执行顺序探究

官方说明
鸿洋博客Fragment使用教程

Fragment在开发中是可用以替代Activity的一种轻量级界面,它的生命周期如下图所示
Activity与Fragment同时运行时生命周期执行顺序探究_第1张图片
与Activity对应的生命周期如下图所示
Activity与Fragment同时运行时生命周期执行顺序探究_第2张图片
现要研究的是当Activity的生命周期变化会让Fragment的生命周期发生什么样的变化.
在activity的layout中放置一个fragment,让其在界面打开时自动加载,然后再放置一个framelayout用以动态加载fragment,观察两者的生命周期与Activity间关联。
布局如下:


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.FragmentNormalActivity">

    <fragment
        android:id="@+id/fragment_title"
        android:name="cn.pigdreams.blogdemo.fragment.TitleFragment"
        android:layout_width="match_parent"
        android:layout_height="94dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline4"
        app:layout_constraintVertical_bias="1.0" />
    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />
android.support.constraint.ConstraintLayout>

Activity与Fragment同时运行时生命周期执行顺序探究_第3张图片

1.首次打开界面时

09-04 21:34:48.396 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onCreate
09-04 21:34:48.608 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onAttach
09-04 21:34:48.609 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreate
09-04 21:34:48.617 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreateView
09-04 21:34:48.701 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onActivityCreated
09-04 21:34:48.740 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onAttach
09-04 21:34:48.741 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onCreate
09-04 21:34:48.743 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onCreateView
09-04 21:34:48.762 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onActivityCreated
09-04 21:34:48.763 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStart
09-04 21:34:48.764 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onStart
    Activity:onStart
09-04 21:34:48.773 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onResume
09-04 21:34:48.775 6975-6975/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onResume
    Fragment:示例1A->onResume

  • 启动时一般都是先执行Activity的生命周期,再执行Fragment的
  • 除了Fragment的onStart()先于Activity的onStart()先执行这一个特例
  • 正常启动时每个Fragment的onAttach->onActivityCreated这四个生命周期一般都是一组一起执行的,在执行完这一组方法前不会去执行其它Fragment的生命周期方法

Activity与Fragment同时运行时生命周期执行顺序探究_第4张图片

2.Home键回到主界面时又返回时

09-04 21:37:56.725 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onPause
    Fragment:Title->onPause
    Fragment:示例1A->onPause
09-04 21:37:56.749 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onSaveInstanceState
09-04 21:37:56.751 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onSaveInstanceState
    Fragment:示例1A->onSaveInstanceState
09-04 21:37:56.767 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onStop
09-04 21:37:56.768 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStop
    Fragment:示例1A->onStop
09-04 21:38:14.097 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onRestart
09-04 21:38:14.101 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStart
    Fragment:示例1A->onStart
    Activity:onStart
09-04 21:38:14.102 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onResume
09-04 21:38:14.104 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onResume
09-04 21:38:14.105 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onResume
  • onSaveInstanceState()保存状态的方法是在onPause()之后,onStop()之前,Activity与Fragment在这里是同步的
  • 再次从最近活动返回时,activity会多执行一个onRestart()方法,Fragment无此方法
  • Activity#onStart()始终处于Fragment#onStart()后面执行
  • Fragment的onResume()是最后执行的

Activity与Fragment同时运行时生命周期执行顺序探究_第5张图片

3.正常销毁时

09-04 21:47:23.314 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onPause
    Fragment:Title->onPause
09-04 21:47:23.315 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onPause
09-04 21:47:23.938 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onStop
09-04 21:47:23.940 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStop
    Fragment:示例1A->onStop
09-04 21:47:23.941 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onDestroy
09-04 21:47:23.943 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onDestroyView
    Fragment:Title->onDestroy
09-04 21:47:23.944 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onDetach
    Fragment:示例1A->onDestroyView
09-04 21:47:23.947 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onDestroy
    Fragment:示例1A->onDetach
  • 正常销毁时(finish)不会执行onSaveInstanceState()这个保存状态的方法
  • 销毁过程Activity的生命周期方法始终在Fragment之前执行
  • Fragment的onDestroyView->onDetach,一起执行

Activity与Fragment同时运行时生命周期执行顺序探究_第6张图片

4.在后台被意外销毁又从最近活动重新回到界面时

09-04 22:45:16.529 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onAttach
09-04 22:45:16.530 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onCreate
    Activity:onCreate
09-04 22:45:16.576 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onAttach
09-04 22:45:16.577 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreate
09-04 22:45:16.578 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreateView
09-04 22:45:16.600 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onActivityCreated
    Fragment:示例1A->onCreateView
09-04 22:45:16.606 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onActivityCreated
09-04 22:45:16.609 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStart
    Fragment:示例1A->onStart
    Activity:onStart
09-04 22:45:16.610 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onRestoreInstanceState
09-04 22:45:16.614 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onResume
09-04 22:45:16.617 9894-9894/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onResume
    Fragment:示例1A->onResume
  • 与正常启动最大的不同在于activity持有的Fragment会优先被还原,最先执行Fragment的onAttach和onCreate方法
  • Activity会额外执行一个还原状态的方法onRestoreInstanceState,介于onStart和onResume之间

Activity与Fragment同时运行时生命周期执行顺序探究_第7张图片

5.横竖屏切换时

切横屏

09-04 21:40:00.502 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onPause
    Fragment:Title->onPause
    Fragment:示例1A->onPause
09-04 21:40:00.504 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onSaveInstanceState
    Fragment:Title->onSaveInstanceState
    Fragment:示例1A->onSaveInstanceState
09-04 21:40:00.517 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onStop
09-04 21:40:00.518 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStop
    Fragment:示例1A->onStop
09-04 21:40:00.519 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onDestroy
    Fragment:Title->onDestroyView
09-04 21:40:00.520 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onDestroy
    Fragment:Title->onDetach
    Fragment:示例1A->onDestroyView
09-04 21:40:00.524 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onDestroy
    Fragment:示例1A->onDetach
09-04 21:40:00.663 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onAttach
    Fragment:null->onCreate
09-04 21:40:00.664 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onCreate
09-04 21:40:00.696 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onAttach
    Fragment:Title->onCreate
09-04 21:40:00.697 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreateView
09-04 21:40:00.719 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onActivityCreated
09-04 21:40:00.721 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onCreateView
09-04 21:40:00.728 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onActivityCreated
    Fragment:Title->onStart
    Fragment:示例1A->onStart
    Activity:onStart
09-04 21:40:00.729 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onRestoreInstanceState
09-04 21:40:00.735 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onResume
09-04 21:40:00.737 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onResume
    Fragment:示例1A->onResume

切竖屏

09-04 21:43:07.771 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onPause
    Fragment:Title->onPause
09-04 21:43:07.772 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onPause
    Activity:onSaveInstanceState
09-04 21:43:07.775 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onSaveInstanceState
09-04 21:43:07.776 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onSaveInstanceState
09-04 21:43:07.789 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onStop
    Fragment:Title->onStop
    Fragment:示例1A->onStop
    Activity:onDestroy
09-04 21:43:07.790 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onDestroyView
09-04 21:43:07.791 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onDestroy
    Fragment:Title->onDetach
    Fragment:示例1A->onDestroyView
09-04 21:43:07.794 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onDestroy
09-04 21:43:07.801 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onDetach
09-04 21:43:07.962 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:null->onAttach
    Fragment:null->onCreate
09-04 21:43:07.965 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onCreate
09-04 21:43:08.016 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onAttach
    Fragment:Title->onCreate
09-04 21:43:08.017 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onCreateView
09-04 21:43:08.030 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onActivityCreated
09-04 21:43:08.031 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onCreateView
09-04 21:43:08.040 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onActivityCreated
09-04 21:43:08.041 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onStart
    Fragment:示例1A->onStart
09-04 21:43:08.043 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onStart
    Activity:onRestoreInstanceState
09-04 21:43:08.067 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Activity:onResume
09-04 21:43:08.077 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:Title->onResume
09-04 21:43:08.078 7308-7308/cn.pigdreams.blogdemo D/BLOG_DEMO: Fragment:示例1A->onResume
  • 这里没有设置任何config配置,可以看到横竖屏切换生命周期方法执行都是一致的,先销毁再创建
  • 生命周期变化相当于,按home回到主页,被意外销毁后重新创建的整个过程
  • 相比正常销毁启动多执行的方法是onSaveInstanceState与onRestoreInstanceState

Activity与Fragment同时运行时生命周期执行顺序探究_第8张图片

你可能感兴趣的:(android进阶)