Android Fragment (ii): Fragment creation and its life cycle

Source: Internet
Author: User

The life cycle of fragments

Each fragments has its own set of lifecycle callback methods and handles its own user input events. The corresponding life cycle can be referred to:

Create a slice (Creating a Fragment)

To create a fragment, you must create a subclass of the fragment (or an existing subclass of it). The Fragment class has code, looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate (), OnStart (), OnPause (), and OnStop (). In fact, if you ' re converting a existing Android application to use fragments, you might simply move code from your Activ Ity ' s callback methods into the respective callback methods of your fragment.

To create a fragment, you must create a subclass of fragment (or a subclass that inherits from it). The code for the Fragment class looks much like activity. It has the same callback functions as the activity, such as OnCreate (), OnStart (), OnPause (), and OnStop (). In fact, if you're using a ready-made Android app instead of fragment, you can simply port the code from the activity's callback function to the respective fragment callback function.

Usually, should implement at least the following lifecycle methods:
In general, you need to implement at least some of the following life cycle methods:
OnCreate ()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment so you want to retain when the FR Agment is paused or stopped and then resumed.
This method is called by the system when the fragment is created. In the implementation code, you can initialize the necessary components that you want to keep in fragment, and re-enable them when the fragment is paused or stopped.

Oncreateview ()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method, which is the root of your fragment ' s layout. You can return NULL if the fragment does is not provide a UI.
This method is called by the system the first time the user interface is drawn for fragment. To draw the user interface for fragment, this function must return the root view of the painted fragment. If fragment does not have a user interface, you can return empty.

OnPause ()
The system calls this method as the first indication, the user is leaving the fragment (though it does don't always mean The fragment is being destroyed). This is usually where you should commits any changes that should be persisted beyond the current user session (because the User might not come back).
The system callback uses this function as the first omen for the user to leave fragment (although this does not always mean that fragment is destroyed). Before the end of the current user session, it is common to commit any changes that should persist here (because the user may no longer return).

Most applications should implement at least these three methods for every fragment, but there is several other callback m Ethods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods is discussed more later, and the section is about handling the Fragment lifecycle.
Most applications should implement these three methods at least for each fragment, but there are many other callback functions that manipulate the various stages of the fragment lifecycle. Callback functions in all life cycles are discussed later in the section on manipulating the fragment life cycle.

There is also a few subclasses that's might want to extend, instead of the base Fragment class:
In addition to the base class fragment, here are a few subclasses you might inherit:

Dialogfragment
Displays a floating dialog. Using this class to create a dialog are a good alternative to using the dialog helper methods in the Activity class, Becaus E You can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to RET Urn to a dismissed fragment.
Displays a floating dialog box. Using this class to create a dialog is a good alternative to using the Activity Class dialog tool method, because you can incorporate the fragment dialog into the fragments background stack managed by the activity, allowing the user to return to an abandoned fragment.

Listfragment
Displays a list of items that is managed by the adapter (such as a simplecursoradapter), similar to listactivity. It provides several methods for managing a list view, such as the Onlistitemclick () callback to handle click events.
Displays a list of entries managed by the adapter (for example, Simplecursoradapter), similar to listactivity. It also provides many functions for managing list views, such as the Onlistitemclick () callback function that handles click events.

Preferencefragment
Displays a hierarchy of Preference objects as a list, similar to preferenceactivity. This was useful when creating a "settings" activity for your application.
Displays a list of architectures for a preference object, similar to preferenceactivity. This is useful when creating "set up" activity for your application.

Copy to Google TranslateTranslation Results

Android Fragment (ii): Fragment creation and its life cycle

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.