Brief analysis of Viewpager&fragment&fragmentactivity

Source: Internet
Author: User

Fragment believe that everyone who writes Android knows it, because there is no time to start contact, so always use activity occasionally using fragment and fragmentactivity, but because of the need for reasons, The landlord had to go deep understanding fragment, because some things are activity can not be realized, is passive to understand the new things, this habit is not very good, it is estimated because do not understand his good aspects, it was very repulsive to use him, But now feel this goods with this viewpager really very useful, both to simplify the code, but also to reduce the coupling of the program, it is very handy to use it very convenient. Okay, the usual, on the code.

I did not list all the fragment life cycles, and listed a few very common

Onattach,oncreate,oncreateview,onviewcreated,onconfigurationchanged,oncreateview,onactivitycreated,onstart, Onresume,onpause,onstop,ondestroyview,ondetach,setcontentview,onhiddenchanged. Bo Master Simple List of these methods, below to explain in detail

Because bloggers use the Android studio developed, so new fragment when it will automatically give you to build an interface implementation, Bo master with less, but also to mention, this method simplifies the interaction between different fragment, you can wear a URL parameter, Specific use according to the actual situation, but if not note off and not the program is not run up, so suggest not to first note off, but do not delete, just in case.

Bo Main write is the company's engineering, all the code is not posted out, the code, the first program first interface is Fragment+fragmentactivity+tabhost, but it contains viewpager+fragment

When this program is opened, the life cycle of the fragment is that this piece of the main research fragmentactivity in fragment

The life cycle is written in the fragmentactivity, not the small module that is combined with the Viewpager.

This shows that when you open the app, it creates your fragmentactivity and then starts loading fragment in this fragmentactivity, And then there's fragment's children, but only for the first show of the fragment, and then the destruction process.


The program is pause then stop, then destroy the page and then separate fragment

Fragment in the CreateView load XML layout file, in the activitycreated initialization, the rest of the specific circumstances in the specific period, here is not detailed, and then the inside nested sub-fragment

First look at the life cycle


This is to open the life cycle of the fragment of the parent fragment, but limited to the fragment of the display page, not to show it, and look at the changes in the child fragment life cycle of the same class.


It can be seen that the new fragment is first established and then destroyed by the old fragment, but not the parent fragment, but the same level of fragment is destroyed, but in the process did not see the onhiddenchanged method of the call, I'll discuss it in detail after I meet you.

Speaking of the use of Viewpager for different fragment switching problems, if the layout in fragment is XML-defined, but there is no definition in fragment, then fragment is switched to the third fragment to destroy, Otherwise it is not destroyed, but if the layout is defined in fragment, the new resume is destroyed when the new fragment is switched on, but if it is not defined then the first fragment will not be destroyed if it is switched to the second fragment. , Bo Master is code word, good tired, we estimate to digest digestion


Brief analysis of Viewpager&fragment&fragmentactivity

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.