The advantages and effects of Android fragment

Source: Internet
Author: User

One: what is fragment

Fragments, fragments. The aim is to solve the dynamic and flexible UI design for different screen resolutions. A large screen such as a flat screen, such as a mobile phone, is designed so that it has more room to put more UI components, and the extra space to store the UI makes it more interactive, resulting in fragments.

Fragments modify the layout configuration for different screen sizes in active activity (small screens may show one clip at a time, while large screens can show two or more).

Fragment must be written into reusable modules. Because fragment has its own layout, responds to events, has its own life cycle and behavior, you can include different instances of the same fragment in multiple activities. This is especially important for making your interface a perfect experience for users with different screen sizes.

Second, fragment advantages

1.Fragment enables you to separate activity into multiple reusable components, each with its own lifecycle and UI.

The 2.Fragment makes it easy to create dynamic and flexible UI designs that can be adapted to different screen sizes. From your phone to your tablet.

The 3.Fragment is a standalone module that is tightly bound to the activity. Can be dynamically removed, joined, exchanged, etc. in the run.

4.Fragment provides a new way for you to unify your UI on different Android devices.

5.Fragment switch is smooth and light-weight switch.

6.Fragment replaces tabactivity to do navigation, better performance.

7.Fragment added nested Fragment usage in version 4.2 to produce better interface effects.

Fragment do local content update more convenient, originally in order to reach this point to put a number of layouts into an activity inside, now can use more fragment to replace, only when needed to load fragment, improve performance.

The return result can be received from the Startactivityforresult, but view cannot.

The life cycle of fragment

You can think of fragment as a modular component of activity that has its own life cycle

A fragment is a part or a behavior in the interface of an activity. You can combine multiple fragment into one activity to create a faceted interface and you can reuse a fragment in multiple activity. You can think of fragment as a modular activity that has its own life cycle, receives its own events, and can be added or deleted while the activity is running.

Fragment cannot exist independently, it must be embedded in the activity, and the life cycle of fragment is directly affected by the activity in which it resides. For example, when activity is paused, all the fragment that it owns are paused, and when the activity is destroyed, all of its fragment are destroyed. However, when the activity is running (before Onresume (), OnPause (), you can manipulate each fragment individually, such as adding or removing them. When you perform the above transaction for fragment, you can add transactions to a stack that is managed by the activity and each of the stacks is a fragment transaction. With this stack, the fragment transaction can be executed in reverse, so that the return key (navigating backwards) can be supported at the fragment level.

The core life cycle method to invoke when displaying fragment (interacting with the user) is as follows:

1. Call the Onattach (activity) method when associating the fragment object with the activity;

2. When the fragment object is initially created, call the OnCreate (Bundle) method;

3. Oncreateview (Layoutinflater, ViewGroup, Bundle) method is used to create and return the view object associated with the fragment;

4. The Onactivitycreate (Bundle) method tells the fragment object that the activity object it is attached to has completed the execution of the Activity.oncreate () method;

5. The OnStart () method causes the Fragment object to be displayed to the user (after the activity that contains the fragment object is started);

6. Onresume () causes the fragment object to interact with the user (after the activity that contains the fragment object is restarted).

68067040

The advantages and effects of Android 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.