Android fragment details 2: Creating Fragment

Source: Internet
Author: User
Create Fragment

To create a fragment, you must derive a class from the derived class of fragment or fragment. Fragment code is written like activity. It has the same callback method as activity, such as oncreate (), onstart (), onpause () and onstop (). In fact, if you want to change the old program to fragment, you only need to move the code of the activity callback Method to the corresponding method in fragment.

Generally, you need to implement the above lifecycle functions:

Oncreate ():

This method is called when fragment is created. You must initialize the basic components of fragment. See the activity description.

Oncreateview ():

The system calls this method when fragment wants to draw its own interface (before it is actually displayed. This method must return the root control of layout of frament. If this fragment does not provide an interface, it should return null.

Onpause ():

No nonsense, just like activity.

Most programs should implement these three methods at least for fragment. Of course, there are several other callback methods that can be implemented as needed. All lifecycle callback functions are described in detail in the "control fragment lifecycle" section.

Is the lifecycle of fragment (its activity is running ). I already understand it. Don't explain it again?


There are also several ready-made fragemtn Derived classes from which you may need to derive, as listed below:

Dialogfragment

A floating dialog box is displayed. The create class dialog box is the best option to replace the Create activity dialog box, because you can put the fragmentdialog into the return stack of the activity so that the user can return to this dialog box.

Listfragment

Display a list control, just like the listactivity class. It provides many methods to manage the list, such as the onlistitemclick () method to respond to the click event.

Preferencefragment

Displays a list of preference objects, which are the same as preferenceactivity. It is used to create a "set" activity for the program.


Related Article

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.