一、生命周期
![](http://developer.android.com/images/fragment_lifecycle.png)
onAttach(Activity)
called once the fragment is associated with its activity.
onCreate(Bundle)
called to do initial creation of the fragment.
onCreateView(LayoutInflater, ViewGroup, Bundle)
creates and returns the view hierarchy associated with the fragment.
onActivityCreated(Bundle)
tells the fragment that its activity has completed its own
Activity.onCreate()
.
onViewStateRestored(Bundle)
tells the fragment that all of the saved state of its view hierarchy has been restored.
onStart()
makes the fragment visible to the user (based on its containing activity being started).
onResume()
makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause()
fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
fragment不再跟用户交互,因为他的activity已经pause或者其他的一个fragment操作在activity中改变了他
onStop()
fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
fragment不再对用户可见,因为他的activity已经stop或者其他的一个fragment操作在activity中改变了他
onDestroyView()
allows the fragment to clean up resources associated with its View.
允许fragment清理资源相关联的视图
onDestroy()
called to do final cleanup of the fragment's state.
最终清理fragment的状态的时候被调用
onDetach()
called immediately prior to the fragment no longer being associated with its activity.
manager = getFragmentManager();
transaction=manager.beginTransaction();
transaction.replace(R.id.fragment, fragment1);
transaction.commit();
生命周期的执行顺序是,先把上一个fragment的结束的生命周期执行一遍,然后新替换的fragment的开始的生命周期执行一遍
04-12 17:42:24.890: D/Fragment2(13321): onStop
04-12 17:42:24.890: D/Fragment2(13321): onDestroyView
04-12 17:42:24.890: D/Fragment2(13321): onDestroy
04-12 17:42:24.890: D/Fragment2(13321): onDetach
04-12 17:42:24.890: D/Fragment1(13321): onAttach
04-12 17:42:24.890: D/Fragment1(13321): onCreate
04-12 17:42:24.890: D/Fragment1(13321): onCreateView
04-12 17:42:24.890: D/Fragment1(13321): onActivityCreated
04-12 17:42:24.890: D/Fragment1(13321): onStart
04-12 17:42:24.890: D/Fragment1(13321): onResume
transaction = manager.beginTransaction();
if (fragment1 != null && !fragment1.isDetached())//&& !fragment1.isDetached()也许可以不要
transaction.detach(fragment1);
if (fragment2 != null && !fragment2.isDetached())//&& !fragment2.isDetached()也许可以不要
transaction.detach(fragment2);
if (fragment2 == null) {
fragment2 = new Fragment2();
}
if (manager.findFragmentByTag(Fragment2.TAG) == null) {
transaction.add(R.id.fragment, fragment2,Fragment2.TAG);
} else {
transaction.attach(fragment2);
}
transaction.commit();
第一次显示fragment1的时候执行全部的生命周期的方法,Log如下:
04-12 17:57:03.930: D/Fragment1(13913): onCreate
04-12 17:57:03.930: D/Fragment1(13913): onCreateView
04-12 17:57:03.990: D/Fragment1(13913): onActivityCreated
04-12 17:57:04.000: D/Fragment1(13913): onStart
04-12 17:57:04.000: D/Fragment1(13913): onResume
04-12 17:58:04.300: D/Fragment2(13913): onStop
04-12 17:58:04.300: D/Fragment2(13913): onDestroyView
04-12 17:58:04.310: D/Fragment1(13913): onCreateView
04-12 17:58:04.310: D/Fragment1(13913): onActivityCreated
04-12 17:58:04.310: D/Fragment1(13913): onStart
04-12 17:58:04.310: D/Fragment1(13913): onResume
public final boolean isAdded ()
Return true if the fragment is currently added to its activity
public final boolean isDetached ()
Return true if the fragment has been explicitly detached from the UI. That is, FragmentTransaction.detach(Fragment)
has been used on it