Fragment生命周期:
1、与Activity 不同:多了几个方法
只是有几个Activity中没有的新方法,这里需要重点介绍一下:
- onAttach方法:Fragment和Activity建立关联的时候调用。
- onCreateView方法:为Fragment加载布局时调用。
- onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
- onDestroyView方法:Fragment中的布局被移除时调用。
- onDetach方法:Fragment和Activity解除关联的时候调用。
2、从下图可以看出,fragment 生命周期依赖于Activity,关系密切。看代码测试:
1、Fragment1.java:
package com.example.fragmenttest;
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;
public class Fragment1 extends Fragment {
private static final String TAG="FragmentTest";
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
Log.e(TAG, "onAttach");
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment1 ,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
Log.e(TAG, "onStart");
super.onStart();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
Log.e(TAG, "onResume");
super.onResume();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
Log.e(TAG, "onPause");
super.onPause();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
Log.e(TAG, "onStop");
super.onStop();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
Log.e(TAG, "onDestroyView");
super.onDestroyView();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.e(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
Log.e(TAG, "onDetach");
super.onDetach();
}
}
2、MainActivity.java
package com.example.fragmenttest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private static final String TAG="ActivityTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "onCreate()");
}
protected void onStart(){
super.onStart();
Log.e(TAG, "onStart(");
}
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
}
实践截图:
启动:
按下HOME:
按下Back:
重新进入: