Android Fragment life cycle

Source: Internet
Author: User

In Android 3.0, Fragment as an important component, was added in, this class in android.app.Fragment; , you can directly inherit fragment to create a fragment class,

Of course fragment can also be compatible in the lower version, including a fragmentactivity in the Android-support-v4.jar. You can directly inherit fragmentactivity to create a fragment class.

Fragment is mainly used in flat panel, for example: Display list item on the left, detail item on the right side, when the user clicks on each item, the fragment on the right shows the details of the user clicking the item directly.

As shown in the following:

The Fragment life cycle is:

Onattach ();    Oncreat ();   Oncreateview (); onviewcreated ();  onactivitycreated (); OnStart ();  Onresume ();   onPause ();   onStop ();   Ondestroyview (); OnDestroy (); Ondetach ();

See the official Android document image below:

The life cycle of the fragment is very much related to the life cycle of the activity. Changes in the life cycle of the activity containing fragment, including changes in the fragment life cycle

Here's a comparison of the activity of the Android official document with the Fragment life cycle:

when fragment is set in a static manner, that is, by setting it in another control in the layout file, its life cycle changes with the life cycle of the activity. The method invocation process for its life cycle is this way

1, when the layout page where fragment is first appeared, the life cycle of fragment is called as follows:

2, when the phone screen is turned off or the phone screen is dimmed, the life cycle of the fragment is called as follows:

3, when the completion of the above 2 operation. When the phone screen is turned on, or when the phone is lit, the call order is:

4. When the return key is pressed on the screen where the current fragment is located, the order of invocation is:

However, when Fragmentmanager is used to manage fragment and involves addtobackstack, its life cycle becomes more complex.

Here's a look at the problem:

Define the Fraga.java file and the Fragb.java file as follows:

Package Com.example.my;import Java.util.jar.attributes.name;import Android.app.activity;import Android.app.fragment;import Android.os.bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class FragA extends Fragment {public static String TAG = Fraga.cla Ss.getsimplename (); @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.I (TAG, "onCreate");}   @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); LOG.I (TAG, "onactivitycreated");} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {Log.i (TAG, "Oncreateview"); return Super.oncreateview (Inflater, container, savedinstancestate);} @Overridepublic void Onattach (activity activity) {Super.onattach (activity); LOG.I (TAG, "Onattach");} @Overridepublic void OnDestroy () {log.i (TAG, "ondestory"); Super.ondestroy ();} @Overridepublic void Ondetach () {log.i (TAG, "Ondetch"); Super.ondetach ();} @Overridepublic void OnPause () {log.i (TAG, "OnPause"); Super.onpause ();} @Overridepublic void Ondestroyview () {Super.ondestroyview (); LOG.I (TAG, "Ondestroyview");} @Overridepublic void onviewcreated (view view, Bundle savedinstancestate) {super.onviewcreated (view, Savedinstancestate); LOG.I (TAG, "onviewcreated");} @Overridepublic void OnStart () {Super.onstart (); LOG.I (TAG, "OnStart");}}

 fragb.java File:

Package Com.example.my;import Java.util.jar.attributes.name;import Android.app.activity;import Android.app.fragment;import Android.os.bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class FRAGB extends Fragment {public static String TAG = Fragb.cla Ss.getsimplename (); @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.I (TAG, "onCreate");}   @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); LOG.I (TAG, "onactivitycreated");} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {Log.i (TAG, "Oncreateview"); return Super.oncreateview (Inflater, container, savedinstancestate);} @Overridepublic void Onattach (activity activity) {Super.onattach (activity); LOG.I (TAG, "Onattach");} @Overridepublic void OnDestroy () {log.i (TAG, "ondestory"); Super.ondestroy ();} @Overridepublic void Ondetach () {log.i (TAG, "Ondetch"); Super.ondetach ();} @Overridepublic void OnPause () {log.i (TAG, "OnPause"); Super.onpause ();} @Overridepublic void Ondestroyview () {Super.ondestroyview (); LOG.I (TAG, "Ondestroyview");} @Overridepublic void onviewcreated (view view, Bundle savedinstancestate) {super.onviewcreated (view, Savedinstancestate); LOG.I (TAG, "onviewcreated");} @Overridepublic void OnStart () {Super.onstart (); LOG.I (TAG, "OnStart");}}


1, when adding Fraga in the following way:

Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction (); FragA = new FragA (); Fragmenttransaction.replace (R.id.frag_container, FragA, null); Fragmenttransaction.commit ();

Its life cycle is presented in the same way that it is statically set in a layout file. There is not much to say.

2, when we in the following way and show Fraga and no addtobackstack.

@Override public void OnClick (View v) {fragmenttransaction fragmenttransaction = Fragmentmanager.begintransacti        On (); Switch (V.getid ()) {case R.id.button1:if (FragA = = null) {FragA = new Frag                    A (); Fragmenttransaction.replace (R.id.frag_container, FragA, fragnames[0]);//Fragmenttransaction.addtobacks                Tack (fragnames[0]);                    } else {Fragment Fragment = Fragmentmanager.findfragmentbytag (Fragnames[0]);                Fragmenttransaction.replace (R.id.frag_container, Fragment, Fragnames[0]);            } break;                    Case R.id.button2:if (FRAGB = = null) {FRAGB = new FRAGB (); Fragmenttransaction.replace (R.id.frag_container, FRAGB, fragnames[1]);//Fragmenttransaction.addtobacks                Tack (fragnames[1]); } else {Fragment Fragment = fraGmentmanager.findfragmentbytag (Fragnames[1]);                Fragmenttransaction.replace (R.id.frag_container, Fragment, fragnames[1]); <pre class= "java" name= "Code" >}            Break        Default:break;    } fragmenttransaction.commit (); }

The Fraga life cycle invocation order is:

If you click another button to display the FRAGB, see the Fraga and FRAGB life cycle calls below:

We can see it from here. The Fraga call order is OnPause (), OnStop (), Ondestoryview (), Ondetach (), which shows that Fraga has been disposed of Fragmentmanager and replaced by FRAGB's display, If we also press the Back button, then the FRAGB life cycle is the same as calling OnPause (), OnStop (), Ondestoryview (), Ondetach (). This note, if not called addtobackstack () when adding fragment, when Fragmentmanager to replace the fragment, will not save the state of fragment to be replaced.

3, the following when we replace fragment, add Addtobackstack (), then its life cycle will be what? For example:

As a result, it is different to call Addtobackstack () and not to call. What happens to the life cycle of FRAGB and Fraga when the FRAGB is replaced by a button Fraga? Take a look at the picture below:

So it can be seen that the Fraga life cycle method is only called to Ondestroyview, and OnDestroy and Ondetach are not called, which means that the Fraga interface has been destroyed, But Fragmentmanager is not completely destroyed Fraga,fraga still has a state in Fragmentmanager.

then click on Button aand use Fraga to replace the currently displayed FRAGB, at which time the life cycle method call order for Fraga and FRAGB is:

As you can see, the Order of life method invocations of FRAGB is consistent with the order in which Fraga is called when FRAGB replaces Fraga, and the effect is to destroy only the view, but still retain the state of fragment. At this point the call of Fraga is worth noting, at this point Fraga directly from the Oncreateview, that is, just re-create the view, and still use the last time the fragment state was replaced.

OK , at this point, is there a more in-depth understanding of fragment's life cycle method invocation when it addtobackstack?

Okay, one last question. is the life cycle method call for fragment when Fragmentmanager is managed, show and hide.

At this point the call is implemented in the following way:

Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();                Switch (V.getid ()) {case r.id.button1:hideallfrags (fragmenttransaction);                    if (FragA = = null) {FragA = new FragA ();                    Fragmenttransaction.add (R.id.frag_container, FragA, fragnames[0]);                Fragmenttransaction.addtobackstack (Fragnames[0]);                } else {fragmenttransaction.show (FragA);            } break;                Case R.id.button2:hideallfrags (fragmenttransaction);                    if (FRAGB = = null) {FRAGB = new FRAGB ();                    Fragmenttransaction.add (R.id.frag_container, FRAGB, fragnames[1]);                Fragmenttransaction.addtobackstack (Fragnames[1]);                } else {fragmenttransaction.show (FRAGB);            } break; Default:break;       }fragmenttransaction.commit (); 

careful words can be found, in the display of fragment , we used the method add instead of the above use of Replace. and directly addtobackstack. In fact, this can be understood, you think, fragmentmanager in Show or hide, must have existed, or if not, need to add in fragment. This is where show and hide need to be aware that the add and Addtobackstack methods are used.

When you click Button A , the order of the Fraga calls is:

It can be seen that nothing is different from what is said above. Then, when you tap button B , Fraga and FRAGB are called in the following order:

As you can see, FragA does not call the life cycle method, which means that the life cycle of FragA does not change when FRAGB is displayed. The life cycle of the FRAGB is the same as the life cycle method of Fraga when you first click button A.

then continue to click on the buttons a and B, at this point the printed log is:

This means that after Fraga and FRAGB have been added to the backstack, either show or hide, their life cycle is no longer changed.

when the screen is locked or dimmed, and then unlocked or lightened, the FragA and FRAGB life cycle method call Order is:

It can be seen that two fragment have been called OnPause, OnStop, OnStart, Onresume. And the Fraga call will be preceded by FRAGB, which is related to the order in which they were added to the backstack.

This article refers to some classical information out, there are shortcomings in the hope of understanding, correct.


Android Fragment life cycle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.