Android Fragment Usage Summary and introduction

Source: Internet
Author: User

Directory (?) [-] A what is fragment two fragment life cycle three fragment two ways to add Addreplace 42 ways to add performance comparison
View Code

I remember the first contact fragment, feel good cow fork components, can do a lot of activity can do things, auxiliary activity let function can do more powerful, once written, can be used in several places, liberated the activity. Here, this article mainly summarizes the two ways to add fragment, add and replace.

First, what is fragment

Simply put, fragment can actually be understood as a control with its own life cycle, except that the control is a bit special, it has its own ability to handle input events, has its own life cycle, and must rely on activity to communicate and host with each other.

There are several advantages to using fragment:

    • Code reuse. Especially suitable for modular development, because a fragment can be nested by multiple activities, a common business module can be reused, is a good component of the modular UI.
    • Activity is used to manage fragment. The life cycle of fragment is pinned to activity, fragment can be attach added and detach released.
    • Controllable. Fragment can be created and controlled as freely as a normal object, it is easier and easier to pass parameters, and there is no need to deal with system-related things, the display mode, the substitution, whether the whole or the part, can make corresponding changes.
    • Fragments is the view controllers, which contains testable, decoupled business logic blocks, because fragments is built on views, and views are easily animated, so fragments has better control over screen switching.
Second, the life cycle of fragment

The life cycle of the fragment is similar to activity, such as the activity life cycle versus the Fragment Life cycle chart:

Two ways to add fragment (Add&replace)

Fragment add is closely related to Fragmentmanager and fragmenttransaction. Both add and replace are fragmenttransaction methods. In addition to these two methods, there are also remove,hide and show methods.

Fragmentmanager and Fragmenttransaction's acquisition:

// V4, Getsupportfragmentmanager Fragmenttransaction transaction = Frgmentmanager.bengintransatcion ();

1. Add multiple Fragment,add methods in the project

I. Add code

1  Public voidAdd (Baselibfragment fragment,intid,string tag) {2Fragmentmanager Fragmentmanager =((baselibactivity) mcontext). Getsupportfragmentmanager ();3Fragmenttransaction fragmenttransaction =fragmentmanager.begintransaction ();4         //priority check, fragment existence, avoid overlap5Baselibfragment tempfragment =(baselibfragment) Fragmentmanager.findfragmentbytag (tag);6         if(Emptyutils.isnotempty (tempfragment)) {7Fragment =tempfragment;8         }9         if(fragment.isadded ()) {Ten addorshowfragment (fragmenttransaction,fragment,id,tag); One}Else{ A             if(currentfragment!=NULL&&currentfragment.isadded ()) { - fragmenttransaction.hide (currentfragment). Add (ID, Fragment,tag). commit (); -}Else{ the Fragmenttransaction.add (ID, Fragment,tag). commit (); -             } -Currentfragment =fragment; -         } +     } -     /** + * Add or Show fragment A      * at      * @paramFragment -      */ -     Private voidAddorshowfragment (fragmenttransaction transaction, baselibfragment fragment,intid,string tag) { -         if(Currentfragment = =fragment) -             return; -         if(!fragment.isadded ()) {//If the current fragment is not added, it is added to the Fragment manager in transaction.hide (currentfragment). Add (ID, Fragment,tag). commit (); -}Else { to transaction.hide (currentfragment). Show (fragment). commit (); +         } -Currentfragment.setuservisiblehint (false); theCurrentfragment =fragment; *Currentfragment.setuservisiblehint (true); $}

Ii. Add Order

    • First Add, Hide (hidden) currentfragment, then add (add) New fragment. The life cycle will go according to normal process, oncreate->onresume
    • The second time Add, first hide (hide) currentfragment, in Show (show) Old fragment. The life cycle does not go back, it calls Onhiddenchanged (), shows the display state of the fragment, and we can do some refreshing data operations here.

Iii.add method Fragment overlapping bug solution
Set the tag for fragment, find out if it exists by Findfragmentbytag, and then add

1 // priority check, fragment existence, avoid overlap 2         Baselibfragment tempfragment = (baselibfragment) Fragmentmanager.findfragmentbytag (tag); 3         if (Emptyutils.isnotempty (tempfragment)) {4             fragment = tempfragment; 5         }

2. Add multiple Fragment,replace methods in the project
I. Add code

1  Public void int ID) {2         Fragmentmanager Fragmentmanager = ((baselibactivity) mcontext). Getsupportfragmentmanager ( ); 3         Fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction (); 4         fragmenttransaction.replace (ID, fragment); 5         fragmenttransaction.commit (); 6     }

II. How to add
Add a direct, direct replacement method. In this process, because it is a replacement, the first and second additions make no difference, and the life cycle is re-executed once

Four, two ways to add performance comparison

Standard four King Kong models. Bottom four item, via fragment content switching, this way add vs. replace performance, as shown in the two figure below:

Add method

Replace mode

Android Fragment Usage Summary and introduction

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: 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.