Fragment, also known as fragments, was originally intended to fit a large-screen Android device. But after the advent, many Android developers are very fond of this thing. This thing is very useful, but it is not very easy to use. Let me explain the fragment in Android.
1, the cause of fragment
There are many devices running Android, and the screen size is wide variety. For different screen sizes, developers typically develop a set of source code for a mobile phone, then copy and modify the layout to accommodate large-screen devices, tablets, TVs, etc. To solve this problem, Google launched the fragment. You can use fragment as an integral part of an activity's interface, and even the activity interface can be composed entirely of different fragment, fragment has its own life cycle and receives and processes user events, This eliminates the need to write a bunch of control's event-handling code in the activity. More importantly, you can dynamically add, replace, and remove a fragment.
2. Fragment Life cycle
Fragment must be dependent and activity, so the life cycle of activity can directly affect the life cycle of fragment. This map of the official website is a good illustration of the relationship between the two life cycles:
You can see that fragment has several additional life-cycle callback methods than the activity:
Onattach (Activity)
Called when the fragment is associated with an activity.
Oncreateview (Layoutinflater, Viewgroup,bundle)
Create a view of the fragment
Onactivitycreated (Bundle)
Called when the activity's OnCreate method returns
Ondestoryview ()
and Oncreateview want to correspond, when the fragment view is removed when the call
Ondetach ()
Corresponds to Onattach, called when the fragment associated with the activity is canceled
Note: In addition to Oncreateview, all other methods if you rewrite, you must call the parent class for the implementation of the method,
3, static use of fragment
This is the simplest way to use fragment, fragment as a normal control, written directly in the activity's layout file. Steps:
1, inherit fragment, rewrite Oncreateview decide fragemnt layout
2. Declare this fragment in the activity, just as with the normal view
Here's an example (I use 2 fragment as the activity layout, a fragment for the title layout, and a fragment for the content layout):
Titlefragment Layout file:
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:background= "@android: Color/darker_gray"Android:layout_height= "45DP"> <ImageButtonAndroid:id= "@+id/im_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@android:d rawable/star_on" /> <TextViewandroid:textsize= "25SP"Android:textcolor= "@android: Color/holo_red_dark"android:gravity= "Center"Android:text= "Fragment make title bar"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" /></Relativelayout>
Contentfragment Layout file
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"android:gravity= "Center"Android:layout_height= "Match_parent"> <TextViewandroid:gravity= "Center"android:textsize= "25SP"Android:text= "Fragment as the main panel"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></Relativelayout>
Mainactivity Layout file
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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" > <FragmentAndroid:id= "@+id/fg_title"Android:layout_width= "Match_parent"Android:layout_height= "45DP"Android:name= "App.linfeng.com.myapplication.TitleFragment" /> <FragmentAndroid:layout_below= "@id/fg_title"Android:id= "@+id/fg_content"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:name= "App.linfeng.com.myapplication.ContentFragment" /></Relativelayout>
Titlefragment Source Code
Packageapp.linfeng.com.myapplication;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageButton;ImportAndroid.widget.Toast; Public classTitlefragmentextendsFragment {PrivateImageButton Im_button; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View= Inflater.inflate (R.layout.title_fragment_layout,container,false); Im_button=(ImageButton) View.findviewbyid (R.id.im_button); Im_button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (GetContext (),"The business logic is written on the fragment, isn't the activity very neat?" ", Toast.length_short). Show (); } }); returnview; }}
Contentfragment Source Code
Packageapp.linfeng.com.myapplication;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classContentfragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View= Inflater.inflate (R.layout.content_fragment_layout,container,false); returnview; }}
Mainactivity Source Code
Packageapp.linfeng.com.myapplication;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;/*** Because Android Studio new activity is automatically inherited appcompatactivity, so I did not change to * Activity, this is not related to this case ha. */ Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //requestwindowfeature (Window.feature_no_title); //because the inheritance is appcompatactivity, so Requestwindowfeature () failedGetsupportactionbar (). Hide (); Setcontentview (R.layout.activity_main); }}
Finally, let's look at the running effect
Static use of fragment in fact is the fragment as a normal view of the layout of the activity in the file, and then all the control of the event processing and other code by the respective fragment to deal with, instantly feel activity good clean have wood ~ ~ The readability, reusability, and maintainability of the code is not an instant boost
Android Fragment usage details (1)--Static use fragment