Fragment life cycle of "Android development"

Source: Internet
Author: User

  Last blog we talked about the basic use of fragment, I believe everyone has learned how to use. If you do not know the classmate can go to see, the portal. Now let's explain the life cycle of fragment.

  first, the fragment affairs

Before talking about the fragment life cycle, be sure to talk about Fragmenttransaction, which is fragment, which is important for us to understand the life cycle.

So what is a transaction? Depending on the user's interaction, adding (add), removing (remove), replacing (replace), and performing other actions on the fragment, each set of changes submitted to the activity, is referred to as a transaction. There is a stack of data structures in the transaction, which allows the user to navigate back through the changes in the fragment (similar to the activity bounce, which is the same as the jump implementation between the activity, and is implemented as a stack). Users can add fragmenttransaction by adding them. addtobackstack (NULL) function ( must be called before Fragmenttrasaction.commit () ) to add this fragment to the background stack. After understanding the transaction, let's look at the life cycle of the fragment.

Second, fragment life cycle

The previous article mentions that we regard fragment as a "small activity", believing that the students who know the activity are aware that it is necessary to master the life cycle of the activity in the development of Android. Here's a fragment life cycle diagram for everyone, and a few small examples to understand the fragment life cycle in depth.

      

First, we create a Fragment1 ( without adding it to the background stack , that is, not calling the Fragmenttransaction.addtobackstack (NULL) function) and displaying it, viewing the Logcat:

    

We find that when Fragment1 is created, the order of the calls is:

Fragment1:onattach--"-" activity of onattachfragment--"oncreate--" oncreateview--"onactivitycreated--" onstart--"Onresume

    • Now we use Fragment2 (nor add to the background stack ) to replace Fragment1 with the Replace method, what will happen:

    

Observing Logcat, we found that this time it was executed in the following order:

Fragment1:onpause--"onstop--" ondestroyview--"ondestroy--" Ondetach

Fragment2:onattach--"Activity onattachfragment--" oncreate--"oncreateview--" onactivitycreated--"onstart--" Onresume

Second, if we were to add it to the background stack when we created the Fragment1 (that is, call the Fragmenttransaction.addtobackstack (null) function), what would the result look like? Of course, the order of execution of Fragment1 is the same as the original.

    • Let's take a look at what happens when you replace Fragment1 with Fragment2, which is added to the background stack , using replace:

At this point we see that the order of execution is:

    Fragment1:onpause--"onstop--" Ondestroyview. There is no execution ondestroy--"Ondetach" because we added Fragment1 to the background stack, so we didn't destroy Fragment1

Fragment2:onattach--"Activity onattachfragment--" oncreate--"oncreateview--" onactivitycreated--"onstart--" Onresume

    

    •   Now that we press the back button , Fragment1 will show again:

    

    At this point we see that the order of execution is:

Fragment1:oncreateview--"onactivitycreated--" onstart--"Onresume

Fragment2:onpause--"onstop--" ondestroyview--"ondestroy--" Ondetach

Third,   At this time the reunion asked, if we start Fragment2, did not add him to the background stack it? What happens to his call when you press the back button ? So let's take a look:

    

We can see that the order of execution is:

Fragment1:ondestroy--"Ondetach

Why is that? Isn't it weird? My eyes are blinded by the light! Why not call the Fragment1 display, but destroy it? Because after the return key is pressed, the system will remove the fragment from a background stack and destroy it , so call the ondestroy--Ondetach method here and destroy the Fragment1 instead of showing it!!!

    • Press the back key again:

We can see that the order of execution is:

Fragment2: Activityonpause--"onpause--" Activity onstop--"onstop--" Activity ondestory--"ondestroyview--" ondestory--"Ondetach

   

Third, the source code download

This is the life cycle test source, if the above is still unclear, then you can download the source code, run it yourself. Click I download.

In the next section I will also parse for the detailed use of fragment, welcome to subscribe. My csdn Blog: http://blog.csdn.net/u010049692/article/details/38926561

Fragment life cycle of "Android development"

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.