"Android Basics" Fragment Detailed introduction of Fragment

Source: Internet
Author: User

Fragment was introduced in Android 3.0 (API 11) to support a more dynamic, flexible UI on a large screen, such as on tablets and TVs. Fragment can be seen as nested activity, similar to Activitygroup, but the cost is certainly not activitygroup so large, each fragment manages a view,fragment each other independent, Has its own life cycle, which is the life cycle of the activity. Fragment can be used to solve problems with screen adaptation and dynamic View creation.

A fragment is always embedded in an activity and is a modular view whose life cycle is influenced by the activity life cycle it embeds. For example, activity into the paused state, so many attached to the activity of the fragment will enter the paused state, when the activity is destroyed, all the fragment attached to the activity will be destroyed. Then when the activty into the running state Onresume, you can independently manage each fragment, you can add a fragment, you can remove a fragment. When you perform a fragment transaction (created and displayed), you can put this fragment on the fragment fallback stack, which records all the fragment transactions that you have performed. When you click the Back button, you can take a fragment from the fragment stack to display it.

The life cycle of fragment:

If you are aware of the life cycle of the activity it is easy to understand the fragment life cycle, there are many callback methods like activity lifecycle functions in fragment, which are called by the Android system. The official Fragment life cycle chart is as follows:

    • Oncreat ():
      The system calls this function to create fragment, which is used to create resources used throughout the fragment cycle, such as those used in paused, stoped, or resumed methods.
    • Oncreateview ():
      When fragment needs to draw the user interface for the first time, the system calls this function to finish drawing the UI, you must return a view in this function, the view is the root view of fragment, and returns null if you do not want to fragment display the view.
    • OnPause ():
      When you leave fragment, the system calls this function first, in which you need to save the user's state like the work done in the activity's OnPause method.
In general, an application needs to rewrite at least three functions to complete your application logic.

This article is original, prohibit reprint!

If you are interested in programming, want to know more programming knowledge, solve programming problems, want to learn a certain kind of development knowledge, we have Java master, c++/c Master, Windows/linux Master, Android/ios Master, Please pay attention to my public number: Programmer Interactive Alliance or Coder_online, Daniel Online to provide you with services.

"Android Basics" Fragment Detailed introduction of Fragment

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.