In the Android development process, if you use the navigation bar. Then inevitably you need to use fragment to handle the interface. Idle Nothing, just explain how to use framgent.
Seldom write once. Myself
Shoneworn
Shonewron
Shoneworn
Important thing to say three times.
Life cycle of 1.Fragment
Scenario Demo: Switch to the fragment
11-29 14:26:35.095:d/applistfragment (7649): Onattach
11-29 14:26:35.095:d/applistfragment (7649): onCreate
11-29 14:26:35.095:d/applistfragment (7649): Oncreateview
11-29 14:26:35.100:d/applistfragment (7649): onactivitycreated
11-29 14:26:35.120:d/applistfragment (7649): OnStart
11-29 14:26:35.120:d/applistfragment (7649): Onresume
Screen off:
11-29 14:27:35.185:d/applistfragment (7649): OnPause
11-29 14:27:35.205:d/applistfragment (7649): onsaveinstancestate
11-29 14:27:35.205:d/applistfragment (7649): OnStop
Screen unlock
11-29 14:33:13.240:d/applistfragment (7649): OnStart
11-29 14:33:13.275:d/applistfragment (7649): Onresume
To switch to another fragment:
11-29 14:33:33.655:d/applistfragment (7649): OnPause
11-29 14:33:33.655:d/applistfragment (7649): OnStop
11-29 14:33:33.660:d/applistfragment (7649): Ondestroyview
Switch back to the fragment itself:
11-29 14:33:55.820:d/applistfragment (7649): Oncreateview
11-29 14:33:55.825:d/applistfragment (7649): onactivitycreated
11-29 14:33:55.825:d/applistfragment (7649): OnStart
11-29 14:33:55.825:d/applistfragment (7649): Onresume
Back to Desktop
11-29 14:34:26.590:d/applistfragment (7649): OnPause
11-29 14:34:26.880:d/applistfragment (7649): onsaveinstancestate
11-29 14:34:26.880:d/applistfragment (7649): OnStop
Back to Application
11-29 14:36:51.940:d/applistfragment (7649): OnStart
11-29 14:36:51.940:d/applistfragment (7649): Onresume
Exit the App
11-29 14:37:03.020:d/applistfragment (7649): OnPause
11-29 14:37:03.155:d/applistfragment (7649): OnStop
11-29 14:37:03.155:d/applistfragment (7649): Ondestroyview
11-29 14:37:03.165:d/applistfragment (7649): OnDestroy
11-29 14:37:03.165:d/applistfragment (7649): Ondetach
As you can see, there is a difference in activity.
Use of 2.fragment
When fragment is used, it is important to choose containers reasonably. If the container is not selected. Then you may not be able to achieve the effect you want.
Because fragment is displayed as a view in ViewGroup. That is only part of it. So, if fragment can't cover the entire screen, the original view and the new view will appear together. The following is a container to put fragment in my linear layout. Let's see what happens.
Click Textview1 to create a new fragment2. After each click, Fragment2 did not get stained with the screen. Instead, it keeps going down and down in sequence. After clicking the Back button, the fragment2 pops out of the stack again.
Layout file:
Fragment1.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#32dfda"android:gravity= "Center"Android:text= "TextView1"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/textview2"Android:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#36df0a"android:gravity= "Center"Android:text= "TextView1"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/textview3"Android:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#82dfda"android:gravity= "Center"Android:text= "TextView1"android:textsize= "18SP" /></LinearLayout>
Fragment1.java Code:
Packagecom.ailin.accm.activity;ImportCOM.AILIN.ACCM.R;ImportAndroid.content.Context;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public classFragment1extendsFragment {Privatecontext Context; PublicFragment1 (Context context) { This. Context =context; } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View = Inflater.inflate (R.layout.fragment1,NULL); Initview (view); returnview; } Private voidInitview (view view) {TextView TV1=(TextView) View.findviewbyid (R.ID.TEXTVIEW1); Tv1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Fragment2 fg2=NewFragment2 (context); Fragmenttransaction Fragmenttrans=Getfragmentmanager (). BeginTransaction (); Fragmenttrans.settransition (Fragmenttransaction.transit_fragment_open); Fragmenttrans.add (R.id.container, FG2,"Fg2"); Fragmenttrans.addtobackstack (NULL); Fragmenttrans.commit (); } }); } }
As can be seen from the above, in the linear layout container, fragment2 as a view, was in accordance with container in turn AddView into the. So, What if container is replaced by relative layout? And what would it be like?
As can be seen, this time, in the click Textview1 words, Fragment2, will cover fragment1.
PS: To add a note, container will redraw once when the Fragment2 is added as a view. Therefore, when setting up the Fragment2.xml, it is best to use relative layout relativelayout. Otherwise, your fragment can be displayed normally in tab2, but it is added as a view in Fragment1, is added according to the absolute size. Let's show you the effect.
Fragment1 uses a relative layout as a container. Linear layout is used in Fragment2.xml. Effect look.
But if the fragment2.xml also replaced the relative layout, the effect is the second picture.
Acknowledgement: http://blog.csdn.net/forever_crying/article/details/8238863/
Borrowed a picture of each other. The article is also very well written.
Android--fragment detailed