Android Tenth day Note-fragment

Source: Internet
Author: User

Android Tenth day Note-fragmentFragment (Fragment) I. Introduction to Fragment

*fragment is the API introduced in 3.0, mainly to solve the problem of tablet, big screen phone display

*fragment represents the sub-module of activity, so fragment can be understood as a fragment of activity

*fragment must be embedded in activity for use

Two. Steps to create a fragment
    1. Create a class that inherits the fragment class or its subclasses
    2. Rewrite some of the onxxx methods in fragment
    3. There are generally three ways to rewrite

      The. OnCreate () system creates a fragment object and then callbacks the method, initializing the operation. Oncreateview () The method is recalled when fragment draws the interface component. OnPause () callback this method when the user leaves the fragment

Three use fragment to use activity

* Create a layout to populate fragment

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Sound Size"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Speed"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "3D"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Left channel"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Right channel"/></LinearLayout>

* Create a Fragement public class Soundfragment extends Fragment {

@Nullable @override  Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {    view view= Inflater.inflate (R.layout.sound_item,null);     return view;}

* Main interface layout

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layoutwidth=" Matchparent "android:layoutheight=" matchparent "android:orientation=" vertical "> <!--container--><Framelayoutandroid:id= "@+id/fl_container"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "3" ></FrameLayout><LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button Android:onclick= "Sound"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "Sound"/></linearlayout></linearlayout>

*activity

      /*** Click the sound button to display fragment *@paramv*/      Public voidSound (View v) {//Statement FragmentSoundfragment soundfragment =Newsoundfragment (); //Get fragment ManagerFragmentmanager Manager =Getfragmentmanager (); //Open TransactionFragmenttransaction transaction =manager.begintransaction (); //Replace the frame layout container in the current layoutTransaction.replace (r.id.fl_container,soundfragment); //Commit a transactiontransaction.commit ();}

Four-life cycle
    • Onattach---Used to establish a connection and disconnect with Acivity, which is called once when the fragment is added to the activity
    • OnCreate called when creating a fragment---
    • oncreateview--is called every time the fragment is created
    • Onactivitycreated: Called when the activity where fragment is started is completed
    • OnStart: Called when fragment is started
    • Onresume: Callback When restoring fragment, must be called after OnStart method
    • OnPause: callback When pausing fragment
    • OnStop: Callback When stopping fragment
    • Ondestroyview: Creating and destroying views
    • Ondesctory: Used for initialization and destruction as in activity
    • Ondetach: Remove this fragment from the activity, call this method when the replacement is complete, and use this method after the OnDestroy method
Five activity and fragment life cycle comparison
Activity--------------------Fragment                            onattach ()                            onCreate ()                            Oncreateview () onCreate ()                  Onactivitycreatedonstart ()                   OnStart () onresume ()                 onresume () onPause () OnPause                  () onStop ()                   onStop ()                           Ondestoryview ()                           ondestory () ondestory ()                Ondetach ( )

Android Tenth day Note-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.