主要看两张图,和跑代码
一,Fragment的生命周
二,与Activity生命周期的对比
package com.goso.testapp;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Demonstration of using ListFragment to show a list of items
* from a canned array.
*/
public class FragmentListArray extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("HJJ", "Activity &&&& onCreate...");
// Create the list fragment and add it as our sole content.
if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
ArrayListFragment list = new ArrayListFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("HJJ", "Activity &&&& onStart...");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("HJJ", "Activity &&&& onResume...");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("HJJ", "Activity &&&& onStop...");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("HJJ", "Activity &&&& onPause...");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("HJJ", "Activity &&&& onDestroy...");
}
public static class ArrayListFragment extends ListFragment {
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
Log.e("HJJ", "ArrayListFragment **** onAttach...");
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e("HJJ", "ArrayListFragment **** onCreate...");
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e("HJJ", "ArrayListFragment **** onCreateView...");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("HJJ", "ArrayListFragment **** onActivityCreated...");
String[] array = new String[]{"C++", "JAVA", "PYTHON"};
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, array));
}
@Override
public void onStart() {
// TODO Auto-generated method stub
Log.e("HJJ", "ArrayListFragment **** onStart...");
super.onStart();
}
@Override
public void onResume() {
Log.e("HJJ", "ArrayListFragment **** onResume...");
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onPause() {
Log.e("HJJ", "ArrayListFragment **** onPause...");
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onStop() {
Log.e("HJJ", "ArrayListFragment **** onStop...");
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onDestroyView() {
Log.e("HJJ", "ArrayListFragment **** onDestroyView...");
// TODO Auto-generated method stub
super.onDestroyView();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.e("HJJ", "ArrayListFragment **** onDestroy...");
super.onDestroy();
}
@Override
public void onDetach() {
Log.e("HJJ", "ArrayListFragment **** onDetach...");
// TODO Auto-generated method stub
super.onDetach();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
结果:
onCreate过程 01-22 15:30:28.091: E/HJJ(10315): Activity &&&& onCreate... 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onAttach... 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onCreate... 01-22 15:30:28.115: E/HJJ(10315): ArrayListFragment **** onCreateView... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onActivityCreated... onStart过程 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onStart... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onStart... onResume过程 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onResume... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onResume... onPause过程 01-22 15:31:26.748: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:31:26.748: E/HJJ(10315): Activity &&&& onPause... onStop过程 01-22 15:31:27.638: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:31:27.638: E/HJJ(10315): Activity &&&& onStop... onStart过程 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onStart... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onStart... onResume过程 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onResume... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onResume... onPause过程 01-22 15:32:47.412: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:32:47.412: E/HJJ(10315): Activity &&&& onPause... onStop过程 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onStop... onDestroy过程 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroyView... 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroy... 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDetach... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onDestroy...
场景演示 : 切换到该Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕灭掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解锁
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切换到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切换回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到应用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出应用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
比Activity多了一些生命周期,完整和Activity对接上,大家好好利用。