It is appropriate to switch between interfaces in the same app with fragment, because the activity is more heavyweight
Fragment Lightweight, flexible switching
-------------------------------------------
1. Create and use Fragment
2. Fragment life cycle and related practical applications
3. Create an Activity with a sidebar and use the
4. Create a tabbed Activity and use
5. Fragment use and State preservation
6. Fragment Screen switch
-------------------------------------------
Project Code:
-------------------------------------------
1. Create and use Fragment
* Create an activity with fragment to refactor the fragment into a new file Placeholderfragment.java
* Create another Fragment,anotherfragment.java
* Use the button to achieve two fragment switch
1.1 Add a button btnopenanohterfragmentin Layout fragment_main to open another fragment;
Replace, add, hide, show
Public classPlaceholderfragmentextendsFragment { Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View R Ootview=inflater.inflate (r.layout. Fragment_main, container,false); Rootview.findviewbyid (r.id. Btnopenanohterfragment). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubGetfragmentmanager (). BeginTransaction (). Addtobackstack (NULL)//Back key is supported, otherwise the point returns to exit the app directly.Replace(R.id.container,Newanotherfragment ()). commit (); } }); returnRootview; }}
1.2 Add button btnbackin anotherfragment to return to the previous fragment
Public classAnotherfragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated Method StubView root = inflater.inflate (R.layout.fragment_another, container,false); Root.findviewbyid (r.id. Btnback). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubGetfragmentmanager (). Popbackstack (); } }); returnRoot//Super.oncreateview (Inflater, container, savedinstancestate); }}
2. Fragment life cycle and related practical applications
A lot more than the life cycle of the activity,
Oncreate,oncreateview,onpause is the most common
3. Create an Activity with a sidebar and use the
New Activity:navigation Drawer Activity
* Default effect: A ListView has been added to the Oncreateview to display the data
* Custom Sidebar
4. Create a tabbed Activity and use
5. Fragment use and State preservation
6. Fragment Screen switch
-------------------------------------------
-------------------------------------------
Android Fragment Basics