Android official Getting Started document [16] Creating a fragment code snippet

Source: Internet
Author: User

Android official Getting Started document [16] Creating a Fragment code snippet Creating a Fragment
Create a fragment code snippet

This lesson teaches
1.Create a Fragment Class
2.Add a Fragment to an Activity using XML

You should also read
? Fragments

This lesson teaches you
1. Create a fragment Code snippet class
2. use XML to add a fragment snippet to an activity

You should also read
? Fragment Code snippets

Try it Out
Give it a try.

Download the sample
Fragmentbasics.zip
Download sample
Fragmentbasics.zip

You can think of a fragment as a modular sections of an activity, which have its own lifecycle, receives its own input event s, and which you can add or remove while the activity is running (sort of a "sub activity", so can reuse in diff Erent activities). This lesson shows how to extend the Fragment class using the support Library so your apps remains compatible with devices R Unning system versions as low as Android 1.6.
You can think of a fragment as an activity, it has a modular part of its life cycle, receives its own input events, and can be added or run at the same time to remove (a bit like a "child activity" that you can reuse in different activities). This lesson shows you how to use the support library to keep your application up-to-date with a system version that is running low to Android 1.6 compatible extended fragment code fragment classes.

Before you begin this lesson, you must set up your Android project to use the support Library. If you had not used the support library before, set up your project to use the V4 Library by following the support Librar Y Setup document. However, you can also include the action bar in your activities by instead using the V7 AppCompat Library, which is Compat Ible with Android 2.1 (API level 7) and also includes the Fragment APIs.
Before you start this lesson, you must set up the support libraries used in your Android project. If you have not yet used the support library, set up the project to follow the support library settings document using the V4 library. However, you can also include an API in the active action bar that uses the alternative V7 AppCompat Library, which is compatible with Android2.1 (API7 level), and also includes fragment code snippets.

Create a Fragment Class
Create a fragment Code snippet class


--------------------------------------------------------------------------------

To create a fragment, extend the fragment class, then override key lifecycle methods to insert your app logic, similar to The the-the-would with an Activity class.
To create a fragment, extend the fragment code fragment class, and then override the key life cycle method to put your application logic in a similar manner to the activity class.

One difference when creating a Fragment is so you must use the Oncreateview () callback to define the layout. In fact, the callback of the only need in order to get a fragment running. For example, here's a simple fragment this specifies its own layout:
One difference when creating a fragment code fragment is that you must define the layout using the Oncreateview () callback. In fact, this is the only callback you need to run in order to get a fragment. For example, here is a simple fragment that specifies its own layout:

Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.ViewGroup;

public class Articlefragment extends Fragment {
    @Override
    public View Oncreateview (layoutinflater inflater, ViewGroup container,
         Bundle savedinstancestate) {
       //Inflate the Layout for this fragment
        return Inflater.inflate (r.layout.article_ View, container, false);
   }
}

Just like an activity, a fragment should implement and other lifecycle callbacks so allow you to manage its state as it is a dded or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's OnPause () method is called, any fragments in the activity also receive a call to OnPause ().
Just like an activity, a fragment should implement other life cycle callbacks that allow it to add or remove the state you manage from activity as an activity transition between its life cycle states. For example, when the active OnPause () method is called, any fragment in the activity also receives a call to the OnPause ().

More information about the fragment lifecycle and callback methods are available in the Fragments developer guide.
Detailed information about the fragment lifecycle and callback methods can be provided in the fragment Code snippet Developer's Guide.

Add a Fragment to an Activity using XML
use XML to add a fragment code snippet to an activity

--------------------------------------------------------------------------------

While fragments is reusable, modular UI components, each instance of a Fragment class must being associated with a parent Fr Agmentactivity. You can achieve the association by defining each fragment within your activity layout XML file.
While fragments are reusable, modular UI components, each instance of the fragment class must be associated with the parent fragmentactivity. You can implement this association through each fragment defined in your activity layout XML file.

Note:fragmentactivity is a special activity provided in the support Library to handle fragments on system versions older than API level 11. If the lowest system version you are API level one or higher, then you can use a regular Activity.
Note: Fragmentactivity is provided with support for library processing fragment Code snippet system version below API level 11. If you support the minimum system version API level 11 or later, then you can use regular activity special activities.

Here's an example layout file, adds, fragments to a activity when the device was considered "large" (Specif IED by the large qualifier in the directory name).
Here is an example layout file that adds two fragments of activity when the device's screen is considered "large" (specified by the large qualifier's directory name).

Res/layout-large/news_articles.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

    <fragment android:name= " Com.example.android.fragments.HeadlinesFragment "
               android:id= "@+id/headlines_fragment"
               android:layout_weight= "1"
               android:layout_width= "0DP"
               android:layout_height= "Match_parent"/>

<fragment android:name= "Com.example.android.fragments.ArticleFragment"
Android:id= "@+id/article_fragment"
Android:layout_weight= "2"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"/>

</LinearLayout>

Tip:for more on creating layouts for different screens sizes, read supporting different screen sizes.
Tip: To learn more about different screen sizes to create layouts, read support for different screen sizes.

Then apply the layout to your activity:
Then, the layout is applied to your activity:

Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;

public class Mainactivity extends Fragmentactivity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.news_articles);
}
}

If you ' re using the V7 AppCompat Library, your activity should instead extend actionbaractivity, which is a subclass of Fr Agmentactivity (For more information, read Adding the Action Bar).
If you are using the V7 AppCompat Library, your activity should be extended to actionbaractivity, which is a subclass of fragmentactivity (for more information, see Add Action Bar).

Note:when you add a fragment to an activity layout by defining the fragment of the layout XML file, you cannot remove the Fragment at runtime. If you plan to swap your fragments on and out during user interaction, you must add the fragment to the activity when the Activity first starts, as shown in the next lesson.
Note: When you add a fragment active layout by defining a fragment in the layout XML file, you cannot delete the fragment at run time. If you plan to swap your clips and make user interactions, you must add the fragments to the beginning of the activity's first activity, as in the next lesson.

Next:building a flexible UI
Next page: Build a flexible UI

This article is translated from: https://developer.android.com/training/basics/fragments/creating.html

Android official Getting Started document [16] Creating a fragment code snippet

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.