官方说明
鸿洋博客Fragment使用教程
Fragment在开发中是可用以替代Activity的一种轻量级界面,它的生命周期如下图所示
与Activity对应的生命周期如下图所示
现要研究的是当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>
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
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
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
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
切横屏
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