In the first two blog post respectively introduced the Fragment Foundation and fragment life cycle, but said so much fragment how to use it and why we use fragment? This blog post will mainly discuss these two issues, first of all in the app has this good activity, jump up to have so simple, why do we still use fragment? This is because fragment is more lightweight and more flexible to use than the activity, and switching between the internal interface of a program and using fragment as much as possible instead of activity will make our app run more smoothly and efficiently. It also improves the reusability of the interface. But fragment is also very good at adapting to the multi-size screen.
First look at the chestnut, very simple a small example, as follows:
Experience it and discover that two fragment jump faster than the activity jumps.
The Mainactivity.java code is as follows:
1 /**2 * Mainactivity Main interface3 * @authorCodingblock 2015/09/144 *5 */6 Public classMainactivityextendsactionbaractivity {7 8 @Override9 protected voidonCreate (Bundle savedinstancestate) {Ten Super. OnCreate (savedinstancestate); One Setcontentview (r.layout.activity_main); A if(Savedinstancestate = =NULL) { - Getsupportfragmentmanager () - . BeginTransaction () the. Add (R.id.container,Newmainfragment ()) - . commit (); - } - } +}
The mainactivity first gets the Fragmenttransaction object through the Getsupportfragmentmanager () method, and then loads the mainfragment in with the Add () method, The layout files referenced here are activity_main.xml as follows:
1 <Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/container"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Tools:context= "Com.codingblock.learnfragment.MainActivity"7 Tools:ignore= "Mergerootframe" />
Here is the code for Mainfragment:
1 /**2 * Mainfragment Main fragment3 * @authorCodingblock 2015/09/144 *5 */6 Public classMainfragmentextendsFragment {7 8 Publicmainfragment () {9 }Ten One @Override A PublicView Oncreateview (layoutinflater inflater, ViewGroup container, - Bundle savedinstancestate) { -View Rootview = inflater.inflate (R.layout.fragment_main, container,false); theRootview.findviewbyid (R.id.btn_show_other). Setonclicklistener (NewOnclicklistener () { - - @Override - Public voidOnClick (View arg0) { + Getfragmentmanager () - . BeginTransaction () +. Addtobackstack (NULL)//Add the current fragment to the return stack A. replace (R.id.container,Newotherfragment ()). commit (); at } - }); - returnRootview; - } -}
In this fragment put a button to jump to another fragment, and then through the Fragmenttransaction object's replace () method to let Otherfragment to replace the current fragment, it should be noted here, If you want the program to be able to display the previous fragment in a backward way, you need to add the current fragment to the return stack by Addtobackstack () before replacing it.
Its layout file Fragment_main.xml code is as follows:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context= "Com.codingblock.learnfragment.mainactivity$placeholderfragment" >7 8 <Button9 Android:id= "@+id/btn_show_other"Ten Android:layout_width= "Fill_parent" One Android:layout_height= "Wrap_content" A Android:text= "Jump to another fragment" /> - - </LinearLayout>
The last Otherfragment code is as follows:
1 /**2 * Otherfragment Another fragment3 * @authorCodingblock 2015/09/144 *5 */6 Public classOtherfragmentextendsFragment {7 8 @Override9 PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { TenView Rootview = inflater.inflate (R.layout.fragment_other, container,false); OneRootview.findviewbyid (R.id.btn_back). Setonclicklistener (NewOnclicklistener () { A - @Override - Public voidOnClick (View arg0) { the //launch current fragment from the stack - Getfragmentmanager (). Popbackstack (); - } - }); + returnRootview; - } +}
After the program jumps to this fragment, if you want to return to the previous mainfragment we can click the Back button, or a button can be bound to a click event with Fragmenttransaction Popbackstack () method to launch the current fragment stack.
Its layout file code is as follows:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <TextView8 Android:id= "@+id/tv_show"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:text= "This is another fragment" /> A - <Button - Android:id= "@+id/btn_back" the Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:text= "Back" /> - + </LinearLayout>
At this end, the chestnut is over, and though it is simple, it is clear how fragment is used.
Disclaimer: Welcome Reprint, please attach this article link.
Android Advanced Programming note (vii) Two fragment simple jump example