单独的讲fragment的生命周期的意义并不大,因为其为Activity的一部分,所以必须和Activity的生命周期对比着看才能看出二者各个生命周期方法的调用顺序。
因为fragment分动态和静态两种加载方式,所以会出现两种不同的方法调用顺序。
I.结果演示:
首先我们先来看一下运行结果(我用Log.i()输出每个方法被调用的顺序,Activity类和fragment类都使用同一个String TAG = “MainActivity” 作为标记方便查看对比结果,我只把有用的信息摘下来):
1.动态加载fragment是Activity和fragment的生命周期的对比:
MainActivity onCreate
fragment onAttach + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410e4cd8 + activity的textview =android.widget.TextView@41124f10
fragment onCreate + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410e4cd8 + activity的textview =android.widget.TextView@41124f10
fragment onCreateView + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410e4cd8 + activity的textview =android.widget.TextView@41124f10
fragment onActivityCreated + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410e4cd8 + activity的textview =android.widget.TextView@41124f10
MainActivity onStart
fragment onStart
MainActivity onResume
fragment onResume
fragment onPause
MainActivity onPause
fragment onStop
MainActivity onStop
MainActivity onRestart
MainActivity onStart
fragment onStart
MainActivity onResume
fragment onResume
fragment onPause
MainActivity onPause
fragment onStop
MainActivity onStop
fragment onDestroyView
fragment onDestroy
fragment onDetach
MainActivity onDestroy
--------------------------------------------------------------------------------------------------------------------------------------------------------
2.静态加载fragment是Activity和fragment的生命周期的对比:
fragment onAttach + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410dd668 + activity的textview =null
fragment onCreate + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410dd668 + activity的textview =null
fragment onCreateView + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410dd668 + activity的textview =null
MainActivity onCreate
fragment onActivityCreated + Activity =activity.wyc.com.fragmentdemo0305.MainActivity@410dd668 + activity的textview =android.widget.TextView@4111ec28
MainActivity onStart
fragment onStart
MainActivity onResume
fragment onResume
fragment onPause
MainActivity onPause
fragment onStop
MainActivity onStop
MainActivity onRestart
MainActivity onStart
fragment onStart
MainActivity onResume
fragment onResume
fragment onPause
MainActivity onPause
fragment onStop
MainActivity onStop
fragment onDestroyView
fragment onDestroy
fragment onDetach
MainActivity onDestroy
II.结果分析:
一.动态加载:
动态加载时,Activity的onCreate()调用完,才开始加载fragment并调用其生命周期方法,所以在第一个生命周期方法onAttach()中便能获取Activity以及Activity的布局的组件;
二.静态加载:
1.静态加载时,Activity的onCreate()调用过程中,fragment也在加载,所以fragment无法获取到Activity的布局中的组件,但为什么能获取到Activity呢?
2.原来在fragment调用onAttach()之前其实还调用了一个方法onInflate()【哎妈呀~Inflate这单词怎么看着那么眼熟】,该方法被调用时fragment已经是和Activity相互结合了,所以可以获取到对方,但是Activity的onCreate()调用还未完成,故无法获取Activity的组件;
3.Activity的onCreate()调用完成是,fragment会调用onActivityCreated()生命周期方法,因此在这儿开始便能获取到Activity的布局的组件;
三.两者的生命周期方法的调用顺序:
1.创建时,Activity完成一个步骤,fragment也跟着完成一个步骤;
2.暂停或销毁时,则是fragment完成一个步骤,Activity再完成一个3.步骤;
有点像压栈和弹栈的感觉,先进慢出,因为Activity是fragment的父容器;
III.代码:
MainActivity.java:
package activity.wyc.com.fragmentdemo0305;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.vrinux.fragment.Fragment01;
public class MainActivity extends ActionBarActivity {
private String TAG = "MainActivity";
/**
* 当使用动态加载时,不要注释,并注释activity_main.xml中的fragment标签
* @param savedInstanceState
*/
// private FragmentManager fragmentManager = getFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MainActivity onCreate");
// Fragment01 fragment01 = new Fragment01();
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//
// fragmentTransaction.replace(R.id.fm01id, fragment01, "fragment01Tag");
// fragmentTransaction.commit();
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "MainActivity onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "MainActivity onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "MainActivity onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "MainActivity onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "MainActivity onRestart");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "MainActivity onStart");
}
}
Fragment01.java:
package com.vrinux.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import activity.wyc.com.fragmentdemo0305.R;
/**
* Created by yc on 2015/3/5.
*/
public class Fragment01 extends Fragment {
private String TAG = "MainActivity";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01layout, null);
Log.i(TAG,"fragment onCreateView + Activity =" + getActivity() +" + activity的textview ="+ getActivity().findViewById(R.id.activitytvid));
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i(TAG,"fragment onAttach + Activity =" + getActivity() +" + activity的textview ="+ getActivity().findViewById(R.id.activitytvid));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"fragment onCreate + Activity =" + getActivity() +" + activity的textview ="+ getActivity().findViewById(R.id.activitytvid));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG,"fragment onActivityCreated + Activity =" + getActivity() +" + activity的textview ="+ getActivity().findViewById(R.id.activitytvid));
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG,"fragment onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG,"fragment onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG,"fragment onPause");
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG,"fragment onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG,"fragment onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"fragment onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG,"fragment onDetach");
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下方是一个FrameLayout用于动态加载fragment和一个静态加载fragment的fragment的标签"
android:id="@+id/activitytvid"
android:textSize="20sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fm01id"></FrameLayout>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragmentid"
android:name="com.vrinux.fragment.Fragment01" />
</LinearLayout>
fragment01layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/fragmenttvid"
android:text="我是fragment01"/>
</LinearLayout>