简介
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.
一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。
Fragment依附于Activity,在一个Activity中可以创建很多歌Fragment,打个比喻:一个Activity就像一件完整的“衣服”,一个Fragment就像一块块的“补丁”,“衣服”需要“补丁”的时候,将“补丁“添加到需要的地方。在开发中,我们经常两者结合使用,创建出不一样的效果
一、Activity 生命周期
二、Fragment 生命周期
三、对比图
四、测试代码
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(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...