Fragment的生命周期,先上一个简单地图片
1)activity_main.xml , 里面只有一个 button,主要是2个Fragment之间来回切换
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换Fragment" >
</Button>
</LinearLayout>
2)MainActivity.java ,使用动态的方法加载Fragment
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private boolean flag = true;
private Button button;
private Fragment frag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
if (flag) {
MyFragment2 frag2=new MyFragment2();
beginTransaction.replace(R.id.layout, frag2);
flag=false;
} else {
MyFragment1 frag1=new MyFragment1();
beginTransaction.replace(R.id.layout, frag1);
flag=true;
}
beginTransaction.commit();
}
});
}
private void init() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
MyFragment1 frag1 = new MyFragment1();
beginTransaction.add(R.id.layout,frag1);
beginTransaction.commit();
}
}
3)fragment.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:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
4)MyFragment1.java
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("第一个Fragment");
Log.i("hy", "Fragment1----->onCreateView");
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i("hy", "Fragment1----->onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("hy", "Fragment1----->onCreate");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("hy", "Fragment1----->onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.i("hy", "Fragment1----->onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i("hy", "Fragment1----->onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i("hy", "Fragment1----->onPause");
}
@Override
public void onStop() {
super.onStop();
Log.i("hy", "Fragment1----->onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i("hy", "Fragment1----->onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("hy", "Fragment1----->onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i("hy", "Fragment1----->onDetach");
}
}
5)MyFragment2.java
说明:这两个Fragment都使用的是同一个线性布局文件fragment.xml,在这个Fragment里面详细的讲解了其生命周期的调用时间。
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment2 extends Fragment{
//1.当Fragment被添加到Activity的时候会被调用,丙炔只会被调用一次
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i("hy", "Fragment2----->onAttach");
}
//2.创建Fragment的时候会被调用,并且只会被调用一次
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("hy", "Fragment2----->onCreate");
}
//3.每次创建都会去绘制的Fragment的view组件时会回调该方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("第二个Fragment");
Log.i("hy", "Fragment2----->onCreateView");
return view;
}
//4.当Fragment所在的Activity启动完成后会回调改方法
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("hy", "Fragment2----->onActivityCreated");
}
//5.启动Fragment
@Override
public void onStart() {
super.onStart();
Log.i("hy", "Fragment2----->onStart");
}
//6.恢复Fragment时会被回调;如果已经调用了onstart()方法,那么一定会调用onResume()方法。
@Override
public void onResume() {
super.onResume();
Log.i("hy", "Fragment2----->onResume");
}
//7.暂停Fragment时会被回调
@Override
public void onPause() {
super.onPause();
Log.i("hy", "Fragment2----->onPause");
}
//8.停止Fragment
@Override
public void onStop() {
super.onStop();
Log.i("hy", "Fragment2----->onStop");
}
//9.在销毁Fragment所包含的view组件时被调用,与onCreateView相对应
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i("hy", "Fragment2----->onDestroyView");
}
//10.销毁Fragment时会被回调
@Override
public void onDestroy() {
super.onDestroy();
Log.i("hy", "Fragment2----->onDestroy");
}
//11.Fragment从Activity中删除时会回调该方法,并且只会被调用一次
@Override
public void onDetach() {
super.onDetach();
Log.i("hy", "Fragment2----->onDetach");
}
}
测试:
(1)启动第一个Fragment1
(2)----->屏幕锁屏
(3)解锁
(4)------>切换到Fragment2
(5)------>回到桌面
(6)------>回到应用
(7)------>退出Fragment