Android Advanced Programming Note (V) Fragment Introduction

Source: Internet
Author: User

Fragment is a sub-module of activity introduced in Android 3.0 (API level 11). The original intention is to adapt to the large-screen tablet, we only need to use fragment to group the UI components, modular management, it is easy to run the process of dynamic update activity interface. Fragment must be embedded in the activity's use, although it also has its own life cycle, but its life cycle is controlled by the life cycle of the activity where it resides. It is only when the activity is active that we can fragment by means of a separate operation.

First, several characteristics of fragment:

· Fragment is always an integral part of the activity interface. Fragment can be obtained through the Getactivity () method activity,activity can call Fragmentmanager Findfragmentbyid () or Findfragmentbytag () Method gets the fragment.

· When the activity is running, you can invoke Fragmentmanager's add (), remove (), and replace () methods to dynamically manipulate the fragment.

· One activity can combine multiple fragment at the same time; a fragment can also be reused by multiple activity.

· Fragment can respond to input events and have its own life cycle, but its life cycle is controlled by the activity's life cycle.

Ii. Creation of Fragment

Creating fragment requires inheriting fragment base classes or fragment subclasses, fragment subclasses are: Dialogfragment, listfragment, Preferencefragment or Webviewfragment. Similar to activity, creating fragment requires a number of callback methods, such as OnCreate (), Oncreateview (), OnStart (), Onresume (), OnPause (), OnStop (), and so on.

In general, we will rewrite the following three callback methods in development:

· OnCreate (): Callback This method after the system creates the fragment object, initializing the necessary components.

· Oncreateview (): Callback This method when fragment draws the interface component. The method must return a view, which is the view displayed by fragment.

· OnPause (): Callback the method when leaving fragment.

1      //override change method, return value view will be the component displayed as fragment2 @Override3      PublicView Oncreateview (layoutinflater inflater, ViewGroup container,4 Bundle savedinstancestate) {5         //loading fragment_main.xml layout files6View Rootview = inflater.inflate (R.layout.fragment_main, container,false);7         returnRootview;8}

Third, fragment and activity communication

Created fragment there are two ways to add fragment to your activity by adding it to the activity:

· In the layout file: Use the <fragment.../> element to add the Fragment,android:name property to specify the implementation class for the fragment.

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "Horizontal" >6     <Fragment7         Android:name= "Com.codingblock.myfragment.MyFragment"8 Android:id= "@+id/myfragment"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Match_parent"/> One </LinearLayout>

· In Java: Add fragment through the Add () method of the Fragmenttransaction object.

1@SuppressLint ("Newapi")2@Override3protected voidonCreate (Bundle savedinstancestate) {4     Super. OnCreate (savedinstancestate);5     //Loading Containers6 Setcontentview (r.layout.activity_main);7     //Add Fragment8Getfragmentmanager (). BeginTransaction (). Add (R.id.container,Newmyfragment ()). commit ();9}

Where the Activity_main.xml layout file is as follows:

1 <Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/container"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Tools:context= "Com.codingblock.myfragment.MainActivity"7 Tools:ignore= "Mergerootframe" />

Tip: The Getfragmentmanager () method of the activity can return the BeginTransaction () of the Fragmentmanager,fragmentmanager object method to turn on and return the Fragmenttransaction object.

This blog post only describes the simple use of fragment, which will be followed by a more in-depth discussion of fragment in subsequent posts.

  

Android Advanced Programming Note (V) Fragment 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: 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.