Android:fragment life cycle

Source: Internet
Author: User

First, about FRAGMEMT

1, Android 3.0 introduced fragment, mainly to support more dynamic and flexible user interface design, such as Tablet PC. Fragment allows you to combine and Exchange user interface components without changing the view hierarchy. By dividing the layout of the activity into fragment, you can modify the appearance of the activity while it is running, and keep the changes in the activity stack.

2. Fragment represents the behavior or part of the user interface in activity. Multiple fragment can be combined in one activity to build a multi-pane user interface and reuse fragment in multiple activities. You can think of fragment as a module part of activity that has its own life cycle and accepts its own input events that you can add or remove while the activity is running.

3, a fragment must always be embedded in an activity, the life cycle of the fragment is directly affected by the life cycle of the host activity. For example, when activity is paused, all fragment are so, and when activity is destroyed, all fragment are destroyed. However, when an activity is running, you can independently manipulate each of the fragment, such as adding and deleting them.

Second, fragment life cycle diagram

1. When a fragment is created, it goes through the following States.

    • Onattach ()
    • OnCreate ()
    • Oncreateview ()
    • Onactivitycreated ()

2. When the fragment is visible to the user, it will experience the following States.

    • OnStart ()
    • Onresume ()

3. When this fragment enters "background mode", it will experience the following status.

    • OnPause ()
    • OnStop ()

4. When the fragment is destroyed (or the activity that holds it is destroyed), it goes through the following States.

    • OnPause ()
    • OnStop ()
    • Ondestroyview ()
    • OnDestroy ()
    • Ondetach ()

Iii. Creation of Fragment

To create a fragment, you must create a fragment subclass (or one of its existing subclasses).

The code for the Fragment class looks much like an activity. It contains a callback method similar to activity, such as OnCreate (), OnStart (), OnPause (), and OnStop ().

In fact, if you convert an existing Android application to use fragment, you might transfer the code from the activity's callback method to the corresponding callback method of your fragment.

In general, you should implement at least the following life cycle methods:
OnCreate ()
When the fragment is created, the system calls the. In your implementation, you should initialize the fragment you want to keep, and then start again when you pause or stop.

Oncreateview ()
The system calls this time to draw its user interface for the first time. To draw a user interface for your fragment, you must return a view from this method, which is the root of your fragment layout. If fragment does not provide a user interface, you can return null.

OnPause ()
The system calls the method a user to leave the fragment (though not always means that the fragment is destroyed); Usually you should commit any changes made by the current user (because the user may not come back).

The instance code is as follows:

 Public classPhonefragment extends Fragment {@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_phone, container,false); returnview; } @Override Public voidonactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); } @Override Public voidOnDestroy () {Super.ondestroy (); }}

Android:fragment life cycle

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.