Android--fragment detailed

Source: Internet
Author: User

Fragment (fragmentation) was introduced as part of the release of android3.0 (API level 11), and Fragment allows activity to be split into reusable components that are completely separate packages, each with its own lifecycle and UI layout.

Fragment contains a series of event handlers that resemble the Acitivity class. These event handlers are triggered when fragment is created, started, resumed, paused, stopped, and destroyed. Fragment also contains some additional callback to identify the binding and binding relationships between fragment and its parent acitivity, fragment the Creation (and destruction) of the view hierarchy, And the completion of its parent activity's creation process. As shown in the following:

The program manifest shows the basic methods available in a fragment life cycle, describing the events for each state change in the comments for each of the basic methods, and you should consider the actions you want to do

 Packagecom.example.todolist;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classMyfragmentextendsfragment{//when the method is called, fragment is connected to its parent activity.@Override Public voidOnattach (activity activity) {//TODO auto-generated Method Stub        Super. Onattach (activity); //get a reference to the parent activity    }     //call this method to make the initial creation of the fragment@Override Public voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate); //Initialize Fragment    }     //This method is called once the fragment has been created to create its own user interface@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated Method Stub//Create, or populate the Fragment UI, and return it//If this fragment does not have a UI, then return null        return Super. Oncreateview (Inflater, container, savedinstancestate); }      //once the UI of the parent activity and fragment has been created, the method is called@Override Public voidonactivitycreated (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. onactivitycreated (savedinstancestate); //complete initialization of the fragment--especially after the parent activity is initialized or the fragment view is fully populated .    }    //called at the beginning of the visible life cycle@Override Public voidOnStart () {//TODO auto-generated Method Stub        Super. OnStart (); //apply all required UI changes, now fragment is visible    }         //called at the beginning of the activity life cycle@Override Public voidOnresume () {//TODO auto-generated Method Stub        Super. Onresume (); //resuming all paused fragment requires a UI update, thread or process, but is inactive it is paused    }    //called at the end of the activity life cycle@Override Public voidOnPause () {//TODO auto-generated Method Stub        Super. OnPause (); //When activity is not the active foreground activity, you need to pause the UI update, suspend the thread, or pause the centralized processing of those CPUs that do not need to be updated. //after this method is called, the process may be terminated, so all editing and state change information will be maintained.    }         //at the end of the activity lifecycle, call this method to keep the state of the UI changing@Override Public voidonsaveinstancestate (Bundle outstate) {//TODO auto-generated Method Stub        Super. Onsaveinstancestate (outstate); //Save the UI's state change information to Outstate//This bundle is passed to Oncreate,oncreateview and Onactivitycreate (if its parent activity is terminated and restarted) in the method    }    //call this method at the end of the visible life cycle@Override Public voidOnStop () {//TODO auto-generated Method Stub        Super. OnStop (); //pauses the rest of the UI update when fragment is not visible, suspends the thread, or pauses processing that is no longer needed    }    //when the fragment view is detached, the method is called@Override Public voidOndestroyview () {//TODO auto-generated Method Stub//Clear resource-related view        Super. Ondestroyview (); }         //call this method at the end of the entire life cycle@Override Public voidOnDestroy () {//TODO auto-generated Method Stub//clear all resources, including end thread and close database connection, etc.        Super. OnDestroy (); }    //When the fragment is detached from its parent activity, use this method@Override Public voidOndetach () {//TODO auto-generated Method Stub        Super. Ondetach (); } }

Demo Application Cut diagram:

Reference: http://www.2cto.com/kf/201403/288477.html

Android--fragment detailed

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.