1. Initial knowledge of fragment
Fragment's Chinese meaning is fragmentation, before Android development is, the user interface switches all use activity switching, which causes the entire application to use the slow and very memory, because activity is a heavyweight component, it is inconvenient to use inside the application. So there was a fragment to solve the problem. Fragment is a convenient, lightweight, activity-based component, so-called activity-based, it is necessary to have activity as a container, fragment can survive. In addition, Fragment can be added and deleted dynamically. So, fragment is a very handy component to develop.
Life cycle of 2.Fragment
Fragment1 and Fragment2
The main interface loads 1 by default, jumps from 1 to 2 o'clock, 2 executes Oncreate-oncreateview, and executes onpause-ondesdory when backward. 1 Execute onpause-ondesdroyview. Step back, 1 execute ondestory.
3. Create a Fragment
ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by Lzc on 16/6/27.*/ Public classFrag2extendsFragment {@Nullable @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { View v= Inflater.inflate (R.layout.fragment2,container,false); V.findviewbyid (R.id.button2). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Getfragmentmanager (). Popbackstack (); } }); returnv; }}
Create a custom class that inherits the Fragment class, and the package is android.support.v4.app.Fragment. There is a default OnCreate method in this class that returns an object of the view type. The value of this object calls the value returned by the inflate method for the Inflater object. The first parameter is the XML layout file that is loaded by this fragment. That is, a fragment corresponds to an XML layout file. After the View object is created, the object is finally returned, and a fragment class is completed.
Mainactivity.java
Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container,NewFrag1 ()). commit (); } }}
Use Fragmentmanager to add fragment to the main layout. (If you want to switch fragment, change the Add method to the Replace method). In addition, the main class that loads fragment is loaded with an XML layout of framlayout. R.id.container the ID of the framlayout in the main layout.
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/ Container
4. Add fragment to the back stack
Since fragment is activity-based, the fragment switch after the same activity, clicking the Back button does not go back to the previous fragment, but returns to the previous activity, or back to the main interface.
Add to the fallback stack of the Android phone fallback key:
Getfragmentmanager (). BeginTransaction (). Addtobackstack (null). Replace (R.id.container,new Frag2 ()). commit ();
Add a button to make fragment fallback:
Getfragmentmanager (). Popbackstack ();
5. Achieve the same activity within the two fragment jump source
Frag1.java
ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by Lzc on 16/6/27.*/ Public classFrag1extendsFragment {@Nullable @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { View v= Inflater.inflate (R.layout.fragment1,container,false); V.findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Getfragmentmanager (). BeginTransaction (). Addtobackstack (NULL). Replace (R.id.container,NewFrag2 ()). commit (); } }); returnv; }}
Fragment1.xml
<?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"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Fragment1"Android:id= "@+id/textview" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Jump Fragment"Android:id= "@+id/button" /></LinearLayout>
Frag2.java
ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by Lzc on 16/6/27.*/ Public classFrag2extendsFragment {@Nullable @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { View v= Inflater.inflate (R.layout.fragment2,container,false); V.findviewbyid (R.id.button2). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Getfragmentmanager (). Popbackstack (); } }); returnv; }}
Fragment2.xml
<?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"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Fragment2"Android:id= "@+id/textview3" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Back"Android:id= "@+id/button2" /></LinearLayout>
Mainactivity.java See figure above
Activity_main.xml:
<?XML version= "1.0" encoding= "Utf-8"?> <Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_below= "@+id/textview2"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true"Android:layout_margintop= "52DP"></Framelayout>
Project Structure
Advanced article-user interface: 1. fragment-Primary Fragment Components