Android Fragment Basics

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.