1, the production and introduction of fragment 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. 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.
2. Fragment Life cycle
Fragment must be dependent and activity, so the life cycle of activity can directly affect the life cycle of fragment.
You can see that fragment has several additional life-cycle callback methods than the activity:
- Onattach (activity) Called when fragment is associated with activity
- Oncreateview (Layoutinflater, Viewgroup,bundle) Create a view of the fragment
- onactivitycreated (Bundle) is called when the activity's OnCreate method returns
- Ondestoryview () and Oncreateview want to correspond, when the Frag Called when the ment view is removed
- Ondetach () corresponds to Onattach when FRAGM Called when ENT associated with 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
- Inherit fragment, override Oncreateview to determine fragemnt layout
- Declare this fragment in activity as if it were a normal view
4, the use of dynamic fragment
Dynamic loading of fragment with Fragmentmanager
5, the fragment family commonly used API
Fragment commonly used in three classes:
-
- Android.app.Fragment is primarily used to define Fragment
- Android.app.FragmentManager is primarily used to operate in activity fragment
- Android.app.FragmentTransaction to ensure that some of the column fragment operation of the atom, familiar with the word business, you will understand ~
A, the way to obtain Fragmentmanage:
Getfragmentmanager ()// V4, Getsupportfragmentmanager (requires V4 package with Android3.0 versions below)
b, the main operation is the method of Fragmenttransaction
Fragmenttransaction transaction = Fm.bengintransatcion ();//Open a transaction
-
- Transaction.add () Add a fragment to the activity
- Transaction.remove () Removes a fragment from the activity, and if the removed fragment is not added to the fallback stack, the fragment instance will be destroyed.
- Transaction.replace () replaces the current with another fragment, which is actually the fit of remove () and then add () ~
- Transaction.hide () Hides the current fragment, is only set to invisible, and does not destroy
- Transaction.show () shows the previously hidden fragment
- Detach () Removes the view from the UI and differs from remove (), at which point the state of fragment is still maintained by Fragmentmanager.
- The Attach () rebuilds the View view, attaches to the UI, and displays.
- Transatcion.commit () Commit a transaction
Note: The commit method must be called before Activity.onsaveinstance (). Otherwise, the activity state will be inconsistent: loss error.
Thank you for your attention, the actual meeting is updated later, but the theory and practice are just as important. Youth is a like smoking. Smoke in the dust. Ash Fall.
Android Fragment (i)