On the creation and life cycle of fragment in Android app development _android

Source: Internet
Author: User

The

Fragment is part of or an action in the interface of an activity. You can combine multiple fragment into an activity to create a faceted interface and you can reuse a fragment in multiple activity. You can think of fragment as a modular activity that has its own life cycle, receives its own events, and can be added or deleted when it is run. The
fragment cannot exist independently, it must be embedded in the activity, and the life cycle of the fragment is directly affected by the activity in which it resides. For example: When an activity is paused, all the fragment it owns are suspended, and when the activity is destroyed, all the fragment it owns are destroyed. However, when the activity runs (after Onresume (), OnPause (), you can individually manipulate each fragment, such as adding or removing them. When you perform the above transaction for fragment, you can add the transaction to a Hoteis, which is managed by the activity, and each of the stacks is a fragment transaction. With this stack, you can reverse fragment transactions so that you can support the "Back" key (navigate backwards) at the fragment level.
When you add a fragment to an activity, it must be placed in the ViewGroup control, and you need to define fragment's own interface. You can declare fragment in the Layoutxml file, the element is:<fragment>; or you can create fragment in your code, and then add it to the ViewGroup control. However, fragment does not necessarily have to be placed in the activity interface, it can be hidden in the background for actvitiy work.
Next, how to use fragment, including how to maintain your state when fragment in the back hoteis of an activity, how to share events with activities and other fragment, how to display them in the action bar of an activity, and so on.


Android introduced fragment from 3.0, mainly to support more dynamic and flexible interface design, such as the application on the tablet. The tablet has a larger screen space than the mobile phone to combine and interact with the interface components. Fragment allows you to do that design without having to deal with the complex changes in the view tree. By dividing the activity's layout into fragment, you can change its appearance as the activity runs and save the changes in the activity's back stack.
For example: Write a read news program, you can use a fragment to display the title list, another fragment display the contents of the selected title, the two fragment are on an activity, side-by-side display. So the two fragment have their own lifecycle and respond to events of interest to them. Instead of using an activity like a mobile phone to display a list of headlines and use another activity to display news content, you can now put both on an activity and show it.

Fragment must be written into reusable modules. Because fragment has its own layout, its own event response, its own lifecycle and behavior, you can include different instances of the same fragment in multiple activities. This is especially important to make your interface a perfect experience for the user in a different screen size. For example, you can start an activity that contains a lot of fragment while the program is running on a large screen, and start an activity that contains a small amount of fragment when running on a smaller screen.
For example--or just the program that read the news--when you detect that a program is running on a large screen, start Activitya, you put the two fragment of the title list and news content in Activitya, and when the program runs on the small screen, it starts the Activitya, But at this point in a only the title list fragment, when a title is selected, Activitya startup Activityb,b contains news content fragment.

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 refer to the following figure:

Create a slice (creating a Fragment)

To create a fragment, you must create a fragment subclass (or inherit from its subclass). The code for the Fragment class looks like an 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 application instead of fragment, you can simply migrate the code from the activity's callback function to its own fragment callback function.
In general, you need to implement at least several life cycle methods:

OnCreate ()
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 maintain in the fragment (the component here refers to things other than view, such as critical data that needs to be displayed in the interface), which can be re-enabled after the fragment is paused or stopped.

Oncreateview ()
The system calls this method 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 fragment that is plotted. If fragment does not have a user interface, it can return null.

OnPause ()
The system callback uses this function as the first omen for the user to leave fragment (although this does not always mean that the fragment is destroyed). Before the end of the current user session, it is common to commit any changes that should be persisted here (since the user may no longer return).

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 lifecycles are discussed later in the section on manipulating the fragment lifecycle.

In addition to the base class fragment, here are a few subclasses you might inherit:

Dialogfragment
displays a floating dialog box. Using this class to create a dialog box is a good alternative to using the Activity Class dialog tool method, because you can incorporate the fragment dialog into the fragments backend stack, which is managed by the activity, allowing the user to return to an abandoned fragment.

Listfragment
displays a list of items managed by the adapter (for example, Simplecursoradapter), similar to listactivity. It also provides a number of functions for managing list views, such as Onlistitemclick () callback functions that handle click events.

Preferencefragment
displays a list of the preference object's architecture, similar to preferenceactivity. This is useful when creating a "setup" activity for an application.

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.