实例讲解Android应用开发中Fragment生命周期的控制
一、Fragment的生命周期初探
因为Fragment必须嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相关的。
如果Activity是暂停状态,其中所有的Fragment都是暂停状态;如果Activity是stopped状态,这个Activity中所有的Fragment都不能被启动;如果Activity被销毁,那么它其中的所有Fragment都会被销毁。
但是,当Activity在活动状态,可以独立控制Fragment的状态,比如加上或者移除Fragment。
当这样进行fragmenttransaction(转换)的时候,可以把fragment放入Activity的backstack中,这样用户就可以进行返回操作。
使用Fragment时,需要继承Fragment或者Fragment的子类(DialogFragment,ListFragment,PreferenceFragment,WebViewFragment),所以Fragment的代码看起来和Activity的类似。
每当创建一个Fragment时,首先添加以下三个回调方法:
onCreate():系统在创建Fragment的时候调用这个方法,这里应该初始化相关的组件,一些即便是被暂停或者被停止时依然需要保留的东西。
onCreateView():当第一次绘制Fragment的UI时系统调用这个方法,该方法将返回一个View,如果Fragment不提供UI也可以返回null。注意,如果继承自ListFragment,onCreateView()默认的实现会返回一个ListView,所以不用自己实现。
onPause():当用户离开Fragment时第一个调用这个方法,需要提交一些变化,因为用户很可能不再返回来。
将Fragment加载到Activity当中有两种方式:
方式一:添加Fragment到Activity的布局文件当中
方式二:在Activity的代码中动态添加Fragment(荐)
第一种方式虽然简单但灵活性不够。添加Fragment到Activity的布局文件当中,就等同于将Fragment及其视图与activity的视图绑定在一起,且在activity的生命周期过程中,无法切换fragment视图。
第二种方式比较复杂,但也是唯一一种可以在运行时控制fragment的方式(加载、移除、替换)。
二、生命周期控制实例
packagecom.goso.testapp; importandroid.app.Activity; importandroid.app.ListFragment; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.ArrayAdapter; importandroid.widget.ListView; /** *DemonstrationofusingListFragmenttoshowalistofitems *fromacannedarray. */ publicclassFragmentListArrayextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); Log.e("HJJ","Activity&&&&onCreate..."); //Createthelistfragmentandadditasoursolecontent. if(getFragmentManager().findFragmentById(android.R.id.content)==null){ ArrayListFragmentlist=newArrayListFragment(); getFragmentManager().beginTransaction().add(android.R.id.content,list).commit(); } } @Override protectedvoidonStart(){ //TODOAuto-generatedmethodstub super.onStart(); Log.e("HJJ","Activity&&&&onStart..."); } @Override protectedvoidonResume(){ //TODOAuto-generatedmethodstub super.onResume(); Log.e("HJJ","Activity&&&&onResume..."); } @Override protectedvoidonStop(){ //TODOAuto-generatedmethodstub super.onStop(); Log.e("HJJ","Activity&&&&onStop..."); } @Override protectedvoidonPause(){ //TODOAuto-generatedmethodstub super.onPause(); Log.e("HJJ","Activity&&&&onPause..."); } @Override protectedvoidonDestroy(){ //TODOAuto-generatedmethodstub super.onDestroy(); Log.e("HJJ","Activity&&&&onDestroy..."); } publicstaticclassArrayListFragmentextendsListFragment{ @Override publicvoidonAttach(Activityactivity){ //TODOAuto-generatedmethodstub Log.e("HJJ","ArrayListFragment****onAttach..."); super.onAttach(activity); } @Override publicvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub Log.e("HJJ","ArrayListFragment****onCreate..."); super.onCreate(savedInstanceState); } @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ //TODOAuto-generatedmethodstub Log.e("HJJ","ArrayListFragment****onCreateView..."); returnsuper.onCreateView(inflater,container,savedInstanceState); } @Override publicvoidonActivityCreated(BundlesavedInstanceState){ super.onActivityCreated(savedInstanceState); Log.e("HJJ","ArrayListFragment****onActivityCreated..."); String[]array=newString[]{"C++","JAVA","PYTHON"}; setListAdapter(newArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,array)); } @Override publicvoidonStart(){ //TODOAuto-generatedmethodstub Log.e("HJJ","ArrayListFragment****onStart..."); super.onStart(); } @Override publicvoidonResume(){ Log.e("HJJ","ArrayListFragment****onResume..."); //TODOAuto-generatedmethodstub super.onResume(); } @Override publicvoidonPause(){ Log.e("HJJ","ArrayListFragment****onPause..."); //TODOAuto-generatedmethodstub super.onPause(); } @Override publicvoidonStop(){ Log.e("HJJ","ArrayListFragment****onStop..."); //TODOAuto-generatedmethodstub super.onStop(); } @Override publicvoidonDestroyView(){ Log.e("HJJ","ArrayListFragment****onDestroyView..."); //TODOAuto-generatedmethodstub super.onDestroyView(); } @Override publicvoidonDestroy(){ //TODOAuto-generatedmethodstub Log.e("HJJ","ArrayListFragment****onDestroy..."); super.onDestroy(); } @Override publicvoidonDetach(){ Log.e("HJJ","ArrayListFragment****onDetach..."); //TODOAuto-generatedmethodstub super.onDetach(); } @Override publicvoidonListItemClick(ListViewl,Viewv,intposition,longid){ Log.i("FragmentList","Itemclicked:"+id); } } }
结果:
onCreate过程
01-2215:30:28.091:E/HJJ(10315):Activity&&&&onCreate... 01-2215:30:28.091:E/HJJ(10315):ArrayListFragment****onAttach... 01-2215:30:28.091:E/HJJ(10315):ArrayListFragment****onCreate... 01-2215:30:28.115:E/HJJ(10315):ArrayListFragment****onCreateView... 01-2215:30:28.123:E/HJJ(10315):ArrayListFragment****onActivityCreated...
onStart过程
01-2215:30:28.123:E/HJJ(10315):Activity&&&&onStart... 01-2215:30:28.123:E/HJJ(10315):ArrayListFragment****onStart...
onResume过程
01-2215:30:28.123:E/HJJ(10315):Activity&&&&onResume... 01-2215:30:28.123:E/HJJ(10315):ArrayListFragment****onResume...
onPause过程
01-2215:31:26.748:E/HJJ(10315):ArrayListFragment****onPause... 01-2215:31:26.748:E/HJJ(10315):Activity&&&&onPause...
onStop过程
01-2215:31:27.638:E/HJJ(10315):ArrayListFragment****onStop... 01-2215:31:27.638:E/HJJ(10315):Activity&&&&onStop...
onStart过程
01-2215:31:57.537:E/HJJ(10315):Activity&&&&onStart... 01-2215:31:57.537:E/HJJ(10315):ArrayListFragment****onStart...
onResume过程
01-2215:31:57.537:E/HJJ(10315):Activity&&&&onResume... 01-2215:31:57.537:E/HJJ(10315):ArrayListFragment****onResume...
onPause过程
01-2215:32:47.412:E/HJJ(10315):ArrayListFragment****onPause... 01-2215:32:47.412:E/HJJ(10315):Activity&&&&onPause...
onStop过程
01-2215:32:47.865:E/HJJ(10315):ArrayListFragment****onStop... 01-2215:32:47.865:E/HJJ(10315):Activity&&&&onStop...
onDestroy过程
01-2215:32:47.865:E/HJJ(10315):ArrayListFragment****onDestroyView... 01-2215:32:47.865:E/HJJ(10315):ArrayListFragment****onDestroy... 01-2215:32:47.865:E/HJJ(10315):ArrayListFragment****onDetach... 01-2215:32:47.865:E/HJJ(10315):Activity&&&&onDestroy...