Like activities, fragments have their own life cycle. After understanding the life cycle of the fragment, we can correctly save the example when the fragment is destroyed and restore it to the previous state when the fragment is rebuilt.
1, use the previous article fragments, add the following code in the Fragment1.java file:
Package Net.zenail.fragments;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 Fragment1 extends Fragment {@Overridepublic view Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated method STUBLOG.D ("Fragment 1", "Oncreateview");//Instantiate Layout file return Inflater.inflate (R.layout.fragment1, container, false);} @Overridepublic void Onattach (activity activity) {//TODO auto-generated method Stubsuper.onattach (activity); LOG.D ("Fragment 1", "Onattach");} @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); LOG.D ("Fragment 1", "onCreate");} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.onactivitycreated (savedinstancestate); LOG.D ("Fragment 1", "onactiVitycreated ");} @Overridepublic void OnStart () {//TODO auto-generated method Stubsuper.onstart (); LOG.D ("Fragment 1", "OnStart");} @Overridepublic void Onresume () {//TODO auto-generated method Stubsuper.onresume (); LOG.D ("Fragment 1", "Onresume");} @Overridepublic void OnPause () {super.onpause (); LOG.D ("Fragment 1", "OnPause");}; public void OnStop () {//TODO auto-generated method Stubsuper.onstop (); LOG.D ("Fragment 1", "OnStop");} @Overridepublic void Ondestroyview () {//TODO auto-generated method Stubsuper.ondestroyview (); LOG.D ("Fragment 1", "Ondestroyview");} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); LOG.D ("Fragment 1", "OnDestroy");} @Overridepublic void Ondetach () {//TODO auto-generated method Stubsuper.ondetach (); LOG.D ("Fragment 1", "Ondetach");}}
2, press CTRL+F11, the Android simulator to switch to landscape mode;
3. Press the F11 key in Eclipse to debug the application on the emulator;
4. When the application is loaded into the emulator, the Logcat window will display the following:
5. Click the Home button on the emulator to display the following output in the Logcat window:
6. Press and hold the home button on the simulator, then click Fragments to launch the application, Logcat display as follows:
7. Finally, click the Back button in the emulator and the Logcat window displays the following output:
8, from the above example, we know that the process of debris experience is as follows:
When fragments are created: Onattach ()-->oncreate ()-->oncreateview ()-->onactivitycreated ();
When the fragment enters background mode: OnPause ()-->onstop ();
When the fragment becomes visible: OnStart ()-->onresume ();
When the fragment is destroyed: OnPause ()-->onstop ()-->ondestroyview ()-->ondestroy ()-->ondetach ();
9. As with activities, fragments can use bundle objects to restore instances of fragmentation in the following states:
OnCreate (), Oncreateview (), onactivitycreated ().