"Android Basics" Fragment detailed Fragment life cycle

Source: Internet
Author: User

The previous article briefly introduced fragment, an article detailing the life cycle of fragment and creating a user interface.

The main function of fragment is to create a view and have a life cycle to manage the creation and destruction of the view. The life cycle of the fragment is similar to the activity's life cycle, and there are some callback methods, and the work you do is to use these life cycle methods to do the right work in the right way.

The fragment life cycle and activity status diagram are as follows:

The left side is the life cycle state of the activity, and the right side corresponds to which life-cycle methods of the fragment are executed next. You can see that the fragment life cycle function and the activity life cycle function are the same for many names, and the corresponding functions are similar, except that in the created state and destroyed state, there are some methods that are added to the activity. Let's talk about these incremental life-cycle approaches:

    • Onattach ():
      Called when the fragment is associated with the activity in which it is located.
    • Oncreatview ():
      When you need to create a view associated with fragment, this method returns a view that is added to the activity's view tree and returns null if you do not want to fragment display a view.
    • Ondestroyview ():
      Called when the view (created in the Oncreatview () method) associated with the fragment is disassociate from the fragment and removed from the view tree. The Oncreatview method is called again the next time fragment needs to display a view.
    • Ondetach ():
      Called when fragment is associated with a previously onattach () activity-relieving relationship.

Similar to activity, fragment can stay in three states for a long time:

    • Resumed:
      The running state of the fragment, at which point fragment is running and can interact with the user, similar to the resumed state of the activity.
    • Paused:
      There are other activity to get the focus, the foreground is running, the activity of fragment is out of focus, and part of it is shown below the foreground activity.
    • Stopped:
      Fragment is no longer visible, the situation may be that fragment's activity is stopped, or fragment removed from the activity to the fragment fallback stack. A stopped state of fragment is not destroyed, is still alive, its state and internal information is recorded and saved by the system, but not visible, not interactive, it is likely to be reclaimed by the system at this time.

Like activity, you can use bundles to record the state of a fragment, and when the activity is destroyed, you need to record the fragment state and restore the fragment state when the activity is recreated. You can save the state of fragment in the Fragment Onsaveinstancestate () callback method, which is restored in the Oncteat (), Oncreatview (), or onactivitycreated () methods.

The biggest difference between activity and fragment in the life cycle is that the fallback stack is independent, the fallback stack of activity is managed by the system, and fragment's fallback stack is managed by the host activity, which means you can control it ( Call Addtobackstack ()).

Note: In fragment, if you want to get a context object, you can call the Getactivity () method, but the Getactivity () method must be called after the fragment is associated with the activity. otherwise getactivity () returns NULL.

These are all fragment's life cycle related knowledge, let's see how to create fragment and add a view to fragment.

To add a view to fragment, you must override the Oncreateview method, create a view in this method, and return the view, which is the root view of the fragment. In this method the system provides you with a Layoutinflater object, through which you can create a view from an XML file with the following code:

publicstaticclassexamplefragmentextendsfragment{     @Override     publicview Oncreateview ( Layoutinflaterinflater,viewgroupcontainer,                             bundlesavedinstancestate) {        //inflate the layout for this Fragment         returninflater.inflate (R.layout.example_fragment,container,false);    }}

The container parameter in the Oncreatview method is a viewgroup, which is passed from the activity and is the parent view to which the fragment view will be embedded. Here you need to pay attention to Layoutinflater's inflate function, where the third argument is false.

Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot) Parameters
Resource ID for the XML layout resource to load (e.g., R.layout.main_page )
Root Optional view to being the parent of the generated hierarchy (if attachtoroot is true), or else simply an object tha T provides a set of layoutparams values for root of the returned hierarchy (if attachtoroot is false.)
Attachtoroot Whether the inflated hierarchy should be attached to the root parameter? If false, Root is a used to create the correct subclass of Layoutparams for the root view of the XML.

Returns

    • The root View of the inflated hierarchy. If Root was supplied and Attachtoroot are true, this is root; Otherwise it is the root of the inflated XML file.

Explain the meaning of the three parameters in this inflate:

Resource: is the layout file ID to parse;

Root: A viewgroup,attachtoroot is true when the root view of the view is parsed from the XML file, if Attachtoroot is false, only provides some layout parameters to the returned Rootview;

Attachtoroot: Determines whether the resolved view is bound to the view provided by the root parameter.

The above is the life cycle of fragment and how to create a fragment view.

Focus on the public platform: Programmer Interaction Alliance (coder_online), you can get the first original technical articles, and (Java/c/c++/android/windows/linux) technology Daniel Friends, online communication programming experience, Get the basics of programming and solve programming problems. Programmer Interactive Alliance, Developer's own home.

"Android Basics" Fragment detailed 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.