Android Tenth day Note-fragmentFragment (Fragment) I. Introduction to Fragment
*fragment is the API introduced in 3.0, mainly to solve the problem of tablet, big screen phone display
*fragment represents the sub-module of activity, so fragment can be understood as a fragment of activity
*fragment must be embedded in activity for use
Two. Steps to create a fragment
- Create a class that inherits the fragment class or its subclasses
- Rewrite some of the onxxx methods in fragment
-
There are generally three ways to rewrite
The. OnCreate () system creates a fragment object and then callbacks the method, initializing the operation. Oncreateview () The method is recalled when fragment draws the interface component. OnPause () callback this method when the user leaves the fragment
Three use fragment to use activity
* Create a layout to populate fragment
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Sound Size"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Speed"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "3D"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Left channel"/><TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Right channel"/></LinearLayout>
* Create a Fragement public class Soundfragment extends Fragment {
@Nullable @override Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { view view= Inflater.inflate (R.layout.sound_item,null); return view;}
* Main interface layout
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layoutwidth=" Matchparent "android:layoutheight=" matchparent "android:orientation=" vertical "> <!--container--><Framelayoutandroid:id= "@+id/fl_container"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "3" ></FrameLayout><LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button Android:onclick= "Sound"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "Sound"/></linearlayout></linearlayout>
*activity
/*** Click the sound button to display fragment *@paramv*/ Public voidSound (View v) {//Statement FragmentSoundfragment soundfragment =Newsoundfragment (); //Get fragment ManagerFragmentmanager Manager =Getfragmentmanager (); //Open TransactionFragmenttransaction transaction =manager.begintransaction (); //Replace the frame layout container in the current layoutTransaction.replace (r.id.fl_container,soundfragment); //Commit a transactiontransaction.commit ();}
Four-life cycle
- Onattach---Used to establish a connection and disconnect with Acivity, which is called once when the fragment is added to the activity
- OnCreate called when creating a fragment---
- oncreateview--is called every time the fragment is created
- Onactivitycreated: Called when the activity where fragment is started is completed
- OnStart: Called when fragment is started
- Onresume: Callback When restoring fragment, must be called after OnStart method
- OnPause: callback When pausing fragment
- OnStop: Callback When stopping fragment
- Ondestroyview: Creating and destroying views
- Ondesctory: Used for initialization and destruction as in activity
- Ondetach: Remove this fragment from the activity, call this method when the replacement is complete, and use this method after the OnDestroy method
Five activity and fragment life cycle comparison
Activity--------------------Fragment onattach () onCreate () Oncreateview () onCreate () Onactivitycreatedonstart () OnStart () onresume () onresume () onPause () OnPause () onStop () onStop () Ondestoryview () ondestory () ondestory () Ondetach ( )
Android Tenth day Note-fragment