Introduction, use and life cycle of Android development fragment

Source: Internet
Author: User

Fragment Official Website Introduction-http://developer.android.com/guide/components/fragments.html

Introduction to 1.Fragment and two editions

Fragment is introduced by Android3.0 (API 11).

Fragment and activity relationship (quote from official website): You can combine multiple fragments in a single activity to build a multi-pane UI and reuse A Fragment in multiple activities

The Fragment is divided into android.app.Fragment (framework version) and Android.support.v4.app.Fragment (support version).

If the app's minimum API is 11, it's best to use Android.app.Fragment.

If the app is compatible with Android versions 2.3 and 2.0, then use Android.support.v4.app.Fragment.

But now the 2.3 market occupancy rate is already very low, the Android API 22 has released, the newly developed app can use the android.app.Fragment completely.

There are some differences in development using android.app.Fragment and Android.support.v4.app.Fragment, the official quote:

The main differences when using this-support version instead of the framework version is:

    • Your activity must extendFragmentActivity
    • You must call to getSupportFragmentManager() get theFragmentManager

When using Android.support.v4.app.Fragment, 1.activity must inherit fragmentactivity 2. When getting Fragmentmanager, Use the Getsupportfragmentmanager () method.

Prone to error: a different fragment in an app uses different fragment versions, which can cause errors when using some methods

The method replace (int, Fragment) in the type fragmenttransaction are not applicable for the arguments (int, anotherfragmen T

The introduction of the fragment version modified to the same will not be an error.

Use of 2.Fragment

Show fragment in activity, replace fragment, and implement return to previous fragment

A. Show placeholderfragment in Mainactivity

1 Importandroid.app.Activity;2 ImportAndroid.os.Bundle;3 4  Public classMainactivityextendsActivity {5 6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten         if(Savedinstancestate = =NULL) { One Getfragmentmanager (). BeginTransaction () A. Add (R.id.container,Newplaceholderfragment ()). commit (); -         } -     } the}

Replace the placeholderfragment with Anotherfragment in B.mainactivity, and the fragment replacement display is achieved by clicking the button.

1  Public classPlaceholderfragmentextendsFragment {2 3      Publicplaceholderfragment () {4     }5 6 @Override7      PublicView Oncreateview (layoutinflater inflater, ViewGroup container,8 Bundle savedinstancestate) {9View Rootview = inflater.inflate (R.layout.fragment_main, container,false);TenRootview.findviewbyid (R.id.button1). Setonclicklistener (NewOnclicklistener () { One              A @Override -              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub theGetfragmentmanager (). BeginTransaction (). Addtobackstack (NULL). Replace (R.id.container,Newanotherfragment ()). commit ();
Addtobackstack (tag) method, the fragment is added to the backstack, the implementation of the return.
- } - }); - returnRootview; + } -}

C. Return from Anotherfragment to Placeholderfragment, by clicking the button, and the Addtobackstack () method in Placeholderfragment, which implements the back operation.

1  Public classAnotherfragmentextendsFragment {2     3 @Override4      PublicView Oncreateview (layoutinflater inflater,5 @Nullable viewgroup container, @Nullable Bundle savedinstancestate) {6         //TODO auto-generated Method Stub7View root=inflater.inflate (r.layout.anotherfragment, container,false);8         9Root.findviewbyid (r.id.btnreturnfragment). Setonclicklistener (NewOnclicklistener () {Ten              One @Override A              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub - Getfragmentmanager (). Popbackstack (); The current fragment pops up Backstack, showing the previous fragment.  the             } -         }); -         returnRoot; -     } +  -  +}

Life cycle of 3.Fragment

Life cycle Map referencing the official website

In the development process, the general implementation of OnCreate (), Oncreateview (), OnPause () method can meet the requirements.

Introduction, use and life cycle of Android development fragment

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.