Android fragement learning notes (2) -- add and manage the fragement Interface

Source: Internet
Author: User

Add fragement

After learning about fragement's life cycle and other simple knowledge, I went to the official documentation to learn more about fragement. To add fragement to our UI, I provided two common methods, the first is to use the <fragement> label declaration in the activity layout XML file, and the second is to add fragement to a viewgroup in Java code, I am used to the first XML layout method. Here I only introduce the first method. Add two fragement vertically in the main activity and use the <fragement> label declaration, each fragement has its own layout, which is part of the activity.

[HTML] view plain
Copy

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: Tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Tools: context = ". mainactivity"
Android: Orientation = "vertical"
Android: Background = "#7ecef4">
<Fragment
Android: Name = "com. example. fragementexam. fragementlist"
Android: Id = "@ + ID/frag_list"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "2"/>
<Fragment
Android: Name = "com. example. fragementexam. fragementdetails"
Android: Id = "@ + ID/frag_detail"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"/>
</Linearlayout>

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Then we need to add an interface for each fragement, write each layout separately, and embed it into the activity. We must implement the oncreateview () method in each fragement class, add layout. If your fragement class inherits listfragement, you do not need to implement oncreateview () because the listview control object is returned by default.

The following section describes how to generate an object from a layout XML file in the fragement class. The layout file is added to the oncreateview () method:

[Java] view plain
Copy

Public class fragementdetails extends fragment {

@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container,
Bundle savedinstancestate ){
// Todo auto-generated method stub
Return Inflater. Inflate (R. layout. frag_detail, container, false );
}

}

Where
Inflater is used to add a view to fragement.
The container is used to store the viewgroup object of fragement's layout.
The savedinstancestate is similar to the same bundle parameter in oncreate () and used to restore the status.

The inflate method of Inflater has the following three parameters:

Int resource: the layout resource ID of fragement.

Viewgroup root: viewgroup that stores layout of fragement

Boolean attachtoroot: whether to add layout to the container when creating the fragement layout.

Fragement Management Method

To manage fragement, we need to use fragementmanager, which can be obtained through getfragement in the activity where fragement is located. fragementmanager is commonly used;

1. Use findfragmentbyid () or findfragmentbytag () to obtain the existing fragment in the activity.

2. Use popbackstack () to pop up fragment members from the back stack of the activity (this can simulate the action triggered by the back key ).

3. Use addonbackstackchangedlisterner () to register a listener to monitor changes in the back stack.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

With fragementmanager, we can also execute transaction operations on fragement, that is, to execute a group of actions at the same time, first obtain an instance of a transaction through begintransaction, and then execute a series of transaction operations, for example, add (), remove (), replace (), and use the commint () method to submit the transaction.

The following code snippet shows a simple transaction operation.
[Java] view plain
Copy

Public class fragmentpreferences extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Getfragmentmanager (). begintransaction ()
. Replace (Android. R. Id. Content, new prefsfragement (). Commit ();
}

Public static class prefsfragement extends preferencefragment {
@ Override
Public void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Addpreferencesfromresource (R. xml. preferences );
}
}
}

It is worth noting that the transaction can only be committed when the activity is in a saved State (such as the onpause and onstop methods). Otherwise, an exception occurs because the fragment status is lost. Use commitallowingstateloss () If you want to commit a transaction that may be lost ().

Source: http://blog.csdn.net/eyu8874521/article/details/8240355

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.