You can think of fragment as a modular part of the activity, having its own lifecycle, getting its own events, and you can add or remove it when the event is running (a bit like the one you can reuse in a different activity). Child activity "). This lesson describes how to use the support library to inherit the fragment class, so your application is still compatible with running system versions less than Android1.6 devices.
Note: If you decide that your application requires a minimum API level of 11 or higher, you do not need to use the support Library to use the fragment and related APIs within Frameword. Note that this course is mainly about using the Support library API, which uses a special package name, and sometimes a slightly different name from the version API included in the platform.
Before you start this session, you must configure your Android project to use the support Library. If you have not used the support library before, follow the Support library setup documentation to configure your project to use the V4 library. However, you can also include the action Bar in your activity
Create fragment
you can think of a fragment as a module area of activity, it has its own lifecycle, receives its own input events, and you can add and remove it when it's running (it's kind of like a child event, You can reuse them in a variety of activity. This lesson explains how to use the support class library to extend fragment so that your application can be compatible with older versions like Android1.6.
Note: If for some reason you decide that your application needs more than 11 API levels, then you don't need to use a support class library, and you can use the fragment class and related APIs built into the framework instead. Note that the focus of this lesson is to use the APIs in the Support class library, which uses a special package signature, and some times the API name will be slightly different than the version included in the platform.
Create Fragment Class
To create a fragment, you inherit the fragment class, then rewrite the critical lifecycle method and insert your application logic into it, similar to the activity class.
One of the differences when creating fragment is that you have to use the Oncreateview () callback to define the layout. In fact, in order to get a running fragment, this is just the only callback method you need. For example, the following 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 a activity,fragment should implement other lifecycle callback methods, allowing you to manage its state in the activity (add or delete) and the state of the activity as it transitions between its lifecycle states. For example, when the OnPause () method of an activity is invoked, any fragment in the activity also receives a call to the OnPause () method.
For fragment lifecycle and effective callback methods, see the Fragments development guide.
Use XML to add a fragment to an activity
Fragment are reusable, modular UI components, and each instance of a fragment class must be associated with an activity that is fragmentactivity by a parent class. This association can be accomplished by defining each fragment within your activity layout XML file.
Note: Fragmentactivity is a special activity that is provided in a support class library, which is used to handle system versions that are fragment before API level 11. If you use a system version that is at least API level 11 or higher, you can use regular activity.
When the screen is considered large enough, the following example layout file adds two fragment to an activity (the file is placed in a directory name that is qualified by large).
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 information on creating layouts for different screen sizes, see support for different screen sizes.
The following are the activity that uses this layout:
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);
}
Note: When you add fragment to the activity layout by defining fragment in the layout XML file, you cannot delete the fragment at run time. If you plan to switch fragment during user interaction, you must add fragment to the activity when the activity is first started.