fragment生命周期和activity生命周期很像,现在我们通过例子来看看在不同的场景会调用生命周期那些方法,创建一个android项目 lifefragment
MainActivity.java
package com.example.fragmentlifecycle;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new LifeFragment());
ft.commit();
Log.e("生命周期","activity---->onCreate");
}
@Override
protected void onDestroy() {
Log.e("生命周期","activity---->onCreate");
super.onDestroy();
}
@Override
protected void onPause() {
Log.e("生命周期","activity---->onCreate");
super.onPause();
}
@Override
protected void onResume() {
Log.e("生命周期","activity---->onCreate");
super.onResume();
}
@Override
protected void onStart() {
Log.e("生命周期","activity---->onCreate");
super.onStart();
}
@Override
protected void onStop() {
Log.e("生命周期","activity---->onCreate");
super.onStop();
}
@Override
protected void onRestart() {
Log.e("生命周期","activity---->onCreate");
super.onRestart();
}
}
LifeFragment.java
package com.example.fragmentlifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LifeFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.e("生命周期","Fragment---->onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("生命周期","Fragment---->onCreateView");
return inflater.inflate(R.layout.lifefragment, null);
}
@Override
public void onAttach(Activity activity) {
Log.e("生命周期","Fragment---->onAttach");
Log.e("LifeFragment","activity="+activity);
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e("生命周期","Fragment---->onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
Log.e("生命周期","Fragment---->onDestroy");
super.onDestroy();
}
@Override
public void onDetach() {
Log.e("生命周期","Fragment---->onDetach");
super.onDetach();
}
@Override
public void onPause() {
Log.e("生命周期","Fragment---->onPause");
super.onPause();
}
@Override
public void onResume() {
Log.e("生命周期","Fragment---->onResume");
super.onResume();
}
@Override
public void onStart() {
Log.e("生命周期","Fragment---->onStart");
super.onStart();
}
@Override
public void onStop() {
Log.e("生命周期","Fragment---->onStop");
super.onStop();
}
@Override
public void onDestroyView() {
Log.e("生命周期","Fragment---->onDestroyView");
super.onDestroyView();
}
}
当第一次运行的时候log信息:
<pre name="code" class="java">11-03 13:35:37.693: E/生命周期(14098): activity---->onCreate
11-03 13:35:37.693: E/生命周期(14098): activity---->onStart
11-03 13:35:37.693: E/生命周期(14098): Fragment---->onAttach
11-03 13:35:37.693: E/生命周期(14098): Fragment---->onCreate
11-03 13:35:37.694: E/生命周期(14098): Fragment---->onCreateView
11-03 13:35:37.697: E/生命周期(14098): Fragment---->onActivityCreated
11-03 13:35:37.697: E/生命周期(14098): Fragment---->onStart
11-03 13:35:37.698: E/生命周期(14098): activity---->onResume
11-03 13:35:37.698: E/生命周期(14098): Fragment---->onResume
activity被挡住生命周期方法