An introductory course in Android application development using fragment _android

Source: Internet
Author: User
Tags static class unique id

Fragment is the new concept of Android Honeycomb 3.0, fragment is known as fragmentation but is very similar to activity, below describes the role and usage of the Android fragment. Fragment used to describe some behavior or part of the user interface in an activity, you can combine multiple fragment to create multiple UI panels in a single action, At the same time, reuse fragment in multiple activity. You can think of fragment as a section of the event, fragment has its own lifecycle, and receives its own input events, and you can add or remove from the running.

A fragment must always be embedded in an activity, while the life cycle of the fragment is affected by activity, for example, when the activity is suspended, Then all fragments in this activity will be released by Destroy. However, when an activity is running like resume, you can manipulate each fragment individually, such as adding or deleting.

Fragment as a new feature of Android 3.0, some features are powerful, such as merging two activity, as shown in

As shown in the figure above, the article title list is represented by activity A, and ACTIVITYB represents the specific content of the article. We can see that two activity is merged into one activity layout through two fragment and has a good display panel for large screen devices such as flat panels. However, because the life cycle of fragment and activity is complex, the following figure represents the life cycle of the fragments:

Activity, fragment respectively contrast:

The two life cycles are very similar and closely related.


Create a fragment you have to create a fragment subclass or existing subclass, like the following code

public static class Androidfragment extends Fragment {
 @Override public
 View Oncreateview (layoutinflater Inflater, ViewGroup container,
        Bundle savedinstancestate) {return 
    inflater.inflate (r.layout.android_ Fragment, container, false);
 }

OnCreate ()
When fragment is invoked when it is created, you should initialize some useful components, such as those that need to be restored when fragment is paused or stopped.

Oncreateview ()
when the system calls fragment the first time the user interface is drawn, if you draw a UI in your fragment you must return to a view of course you can return NULL to represent this fragment without UI.

So how do you add a fragment to the activity? The layout of the activity can be written like this

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" horizontal "
 android:layout_width=" match_parent "
 android:layout_" height= "Match_parent" >
 <fragment android:name= "com.android.cwj.ArticleListFragment"
   @ +id/list "
   android:layout_weight=" 1 "
   android:layout_width=" 0DP "
   android:layout_height=" match_parent "/>
 <fragment android:name= com.android.cwj.ArticleReaderFragment"
   android:id= "@+id/viewer"
   android:layout_weight= "2"
   android:layout_width= "0DP"
   android:layout_height= "Match_parent"/>
</LinearLayout>

Usually fragment as part of the host activity UI, is embedded as part of the entire view hierarchy of the activity. There are 2 ways you can add a fragment to the activity layout:

In the layout document of the activity, declare fragment
You can specify layout attributes (sdk3.0 later) for fragment, just as you would for a view.
The example is an activity with 2 fragment:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" horizontal "
 android:layout_width=" match_parent "
 android:layout_" height= "Match_parent" >
  <fragment android:name= "Com.example.news.ArticleListFragment"
   @+id/list "
   android:layout_weight=" 1 "
   android:layout_width=" 0DP "
   android:layout_height=" Match_ Parent "/>
  <fragment android:name=" com.example.news.ArticleReaderFragment "
   android:id=" @+id/ Viewer "
   android:layout_weight=" 2 "
   android:layout_width=" 0DP "
   android:layout_height=" Match_ Parent "/>
 </LinearLayout>

The Android:name property in <fragment> specifies the fragment class that is instantiated in layout.

When the system creates this activity layout, it instantiates each of the fragment specified in layout and invokes each Oncreateview () method to obtain fragment for each layout. The system will insert the View returned from fragment directly to the location where the <fragment> element resides.

Note: Each fragment requires a unique identifier, and if the activity reboots, the system can be used to recover fragment (and you can also capture fragment to handle transactions, such as removing it).

There are 3 ways to provide an identity for a fragment:

    • Provides a unique ID for the Android:id property.
    • Provides a unique string for the Android:tag property.
    • If none of the above 2 are provided, the system uses the container view ID.

Second, use Fragmentmanager to add fragment to an existing viewgroup.

Fragment can be added to the activity layout at any time when the activity is running. Simply specify a viewgroup that needs to be placed fragment. To manipulate fragment transactions in your activity (for example, add , remove, or replace a fragment), you must use an API from Fragmenttransaction.

You can obtain an fragmenttransaction instance from your activity as follows:

Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();

You can then use the Add () method to add a fragment, specify the fragment to add, and the view to insert.

Examplefragment fragment = new Examplefragment ();
 Fragmenttransaction.add (R.id.fragment_container, fragment); 
Fragmenttransaction.commit ();

The first parameter of add () is the viewgroup to be fragment, specified by the resource ID, and the second is the fragment that needs to be added. Once changed with Fragmenttransaction, in order for the change to take effect, A commit () must be invoked.

Related Article

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.