fragment-First Knowledge _ life cycle

Source: Internet
Author: User

Fragment (fragmentation) allows the acitivity to be split into reusable components that are completely self-contained, with each component having its own lifecycle and UI layout.

The biggest advantage of fragemnt is that you can create dynamic and flexible layouts for devices of different frequency screen sizes.

Fragment life cycle event handlers:

When the method is called, fragment is connected to its parent activity.
@Override
public void Onattach (activity activity) {
Super.onattach (activity);
Get a reference to the parent activity
}

Call this method to make the initial creation of the fragment
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Initialize Fragment
}

This method is called once the fragment has been created to create its user interface
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View inflate = inflater.inflate (r.layout.layout_myfragment, NULL);
Find the corresponding layout
return inflate;
}

Once the UI of the parent activity and fragment has been created, the method is called
@Override
public void onactivitycreated (Bundle savedinstancestate) {
Super.onactivitycreated (savedinstancestate);
Complete initialization of fragment--especially when the parent acitivity is initialized or fragment view is fully populated.
}

Called at the beginning of the visible life cycle
@Override
public void OnStart () {
Super.onstart ();
Now fragment is visible.
}

Called at the beginning of the activity life cycle
@Override
public void Onresume () {
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 void OnPause () {
Super.onpause ();
When activity is not an active foreground activity, you need to pause UI updates, suspend threads, or pause centralized processing of those CPUs that do not need to be updated. Because
After calling this method, the process may be terminated, so all editing and state change information is protected
}

At the end of the activity life cycle, call this method to protect the UI's state changes
@Override
public void Onsaveinstancestate (Bundle outstate) {
Super.onsaveinstancestate (outstate);
Save the UI's state change information to Savedinstancestate
This bundle is passed to OnCreate, Oncreateview, and Onactivitycrete (if its parent activity is terminated and restarted) in the method
}

Call this method at the end of the visible life cycle
@Override
public void OnStop () {
Super.onstop ();
Pauses the rest of the UI update, suspends the thread, or pauses processing that is no longer needed when the fragment is not visible
}

When the fragment view is detached, the method is called
@Override
public void Ondestroyview () {
Super.ondestroyview ();
Clear resource-related view
}

Call this method at the end of the entire life cycle
@Override
public void OnDestroy () {
Super.ondestroy ();
Clear all resources, including end thread and close database connection, etc.
}

When fragment is detached from its parent activity, the method is called
@Override
public void Ondetach () {
Super.ondetach ();
}

Life cycle Graphs:

650) this.width=650; "src=" Http://img.my.csdn.net/uploads/201211/29/1354170699_6619.png "/>

fragment-First Knowledge _ 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.