Android was introduced into fragment at Android 3.0 (API level 11).
Android runs on a wide variety of devices, with small screen phones, oversized flat panels and even TVs. For the screen size gap, in many cases, the first to develop a set of apps for the mobile phone, and then copy, modify the layout to adapt to the plate God horse super large screen. Can't it be that an app can adapt to both the phone and the tablet, of course, must have. The appearance of fragment is to solve such problems. You can think of fragment as a part of an activity interface, even the activity of the interface can be completely different fragment composition, more handsome is fragment have their own life cycle and receive, handle the user's 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.
Since fragment is appearing after Android 3.0, in order to be compatible with the lower version, the general fragment used is the SUPPORT-V4 jar package fragment
It is also worth mentioning that fragment is dependent on activity, and when a fragment creates its corresponding view, a framelayout is used to wrap the view
I. Life cycle of Fragment
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: Except for Oncreateview, all other methods if you rewrite, you must call the parent class for the implementation of the method
Second, load fragment using static method
1. Define a class that inherits from fragment (note that this fragment is under the SUPPORT-V4 package) and implements its Oncreateview method, returning a view that needs to be displayed
public class MyFragment1 extends Fragment {
@Override public View oncreateview (layoutinflater inflater, @Nullable viewgroup container , @Nullable Bundle savedinstancestate) { TextView TextView=new TextView (getactivity ()); Textview.settext ("fragment1111"); Textview.setbackgroundcolor (color.red); return TextView;} }
2. Then in the activity corresponding layout file, register the fragment tag
< Fragment Android:name = "Com.jsako.fragment.MyFragment1" android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1"/>
Ii. loading fragment using dynamic methods (i.e. loading fragment by code)
1, the first step with static method load fragment, are defined as a fragment
2, the second step is different, do not need to re-activity corresponding to the layout of the registration fragment tag, but by acquiring Fragmentmanager, open the transaction to get Fragmenttransaction, by calling
Methods such as Fragmenttransaction.add () and Fragmenttransaction.replace () and fragmenttransaction.remove () enable dynamic loading of fragment, Replace fragment, remove
Fragment
Public classMainactivityextendsfragmentactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=Fragmentmanager. BeginTransaction (); Fragmenttransaction.add (R.id.ll_container,NewMyFragment1 ()). commit (); } PrivateMyFragment2 Fragment2; Public voidReplaceFragment2 (view view) {Fragment2=NewMyFragment2 (); Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=Fragmentmanager. BeginTransaction (); Fragmenttransaction.addtobackstack (NULL); Fragmenttransaction.replace (R.id.ll_container, Fragment2). commit (); } Public voidDeleteFragment2 (view view) {Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=Fragmentmanager. BeginTransaction (); Fragmenttransaction.remove (Fragment2). commit (); }}
Android Fragment Fragment Introduction