Android fragment's use of the encyclopedia _android

Source: Internet
Author: User

Fragment must always be embedded in an activity, and its lifecycle is directly affected by the host activity lifecycle.

The contents of this article can be divided into the following sections:

Using the Support library
Create a fragment
Create a dynamic UI
Communication between multiple fragment

In an activity, a fragment represents an act or part of a user interface. You can combine multiple fragment to create a multiple-panel UI in an activity, and you can reuse a fragment in multiple activity. You can think of a fragment as a module part of an activity, fragment has its own lifecycle, responds to his own input events, and you can add or remove a fragment during the activity run.

1. Use Support Library

If your application needs to run in versions 3.0 and above, you can ignore this part of the content.
If your application uses a version below 3.0, 1.6, or more, you need to use a support library to build it.
To use the Support library:

1. Use the SDK Manager tool under the SDK to download the Android Support Package

2. Create a libs directory under the top-level directory of your Android project

3. Locate the/extras/android/support/v4/android-support-v4.jar under your SDK and copy it to the Libs of your project, select this jar package → right button →build path→add to build P Ath

4. Add under the <manifest> tab of your project's Manifest.xml file:

Copy Code code as follows:

<uses-sdkandroid:minsdkversion= "4"
android:targetsdkversion= "8"/>

Where targetsdkversion is the least supported version of your software

5. If your project supports the version below 3.0, please import the following package: android.support.v4.*;
In the use of fragment activity please inherit fragmentactivity rather than activity. If your system is 3.0 or older, you will also need to import similar packages, but you can use normal activity.

2. Create a Fragment

Fragment support is used in different activity and can handle its own input events and lifecycle methods. Can be seen as a child activity.

Create a fragment

Creating a fragment is similar to creating an activity, inheriting the fragment class, overriding the lifecycle approach, and the main difference is the need to rewrite a Oncreateview () method to return the fragment layout. Example:

The fragment lifecycle approach relies on the life cycle of an activity, such as when the life cycle method of an activity OnPause () is invoked, all the Fragment OnPause () methods in this activity are also invoked.

For more information please refer to class fragment.

Adding fragment to an activity using XML

Although fragment can be reused by multiple activity, you must also associate fragment to a fragmentactivity. You can use the XML layout file to implement this association.

Description: The above mentioned fragmentactivity is applicable to the version of API below 3.0, and 3.0 and above versions can use ordinary activity.
Example:

Using the fragment tag above, android:name= "" Specifies a fragment that is added to the XML. For more information on creating different screen size layouts, please read support for different screen sizes.

When you add fragments to a layout XML file defined by an active layout, you cannot delete fragments at run time. If you intend to interact with the user and swap fragments, you must add active fragments to the activity when the first start.

3. Build a flexible UI

Fragmentmanager provides an action to add, delete, and replace fragment when the activity is running.

You can add fragment instead of defining them in an XML layout file while the activity is running. If you intend to change the life of fragment in an activity.

If you want to perform additions, deletions, and modifications, you must obtain a Fragmenttransaction object through the Fragmentmanager object, and use its APIs to perform these operations.

To add a fragment to an activity, you must add this fragment to a container view. Example:


In the activity you can get the fragment object by Getfragmentmanager () and then through the beginfragmenttransaction of the Fragmentmanager object () method to obtain the Fragmenttransaction object. Add a fragment to the current activity through its add () method.

A Fragmenttransaction object can perform multiple additions and deletions, and if you want to commit these changes to an activity, you must call the object's commit () method at the end. Example:

Because it is not defined in an XML layout, all operations that can transform deletes and modifications.

If you replace or delete a fragment and then allow the user to navigate to the previous fragment, you must call the Addtobackstack () method before calling the commit () method to add to the fallback stack. If you add this fragment to the fallback stack, the fragment will be stop instead of destroyed after the submission. If the user navigates to the fragment, the fragment is restart rather than recreated. If you do not add it to the fallback stack, it will be destroyed when it is deleted or replaced. Example:


4. Interaction with other fragment

There should be no communication between the two individual fragment. They should use the activity of their existence as a communication link.

To implement two fragment interactions, you can define an interface in fragment and then define a method in the interface that invokes the method in the Fragment Onattach () method. Then let the activity implement this method to complete the communication between the activity and the fragment. Example:

To define an interface and invoke the method:


Implements an interface in which you can interact with other fragment data:


You can find a fragment by Fragmentmanager's Findfragmentbyid ().

The above content is small to share the android in the use of fragment, I hope to help you.

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.