Android component: Fragment instance

Source: Internet
Author: User

Android component: Fragment instance

Fragment depends on the Activity, but is independent from the Activity. It can be regarded as a sub-Activity in the Activity, which is generally used on a tablet because the space of an Activity is too large, so it can be divided into several fragments for use, so why is it Fragment. Fragment is also necessary for Software Porting of mobile phones and tablets.

There are two methods to use Fragment. One is to write the code in XML, and the other is to load the code in java. The steps are as follows: 1. Define a class that inherits Fragment and the corresponding xml layout file; 2. Rewrite several necessary methods onCreate () and onCreateView () in the inheritance class (), onPause (), and complete the required operations; 3, in the main Activity or other Fragment in two ways to load the Fragment.

The lifecycle of Fragment is as follows:

The values in red and above are after loading, and the values in red and below are after exiting the current Fragment or Activity.

The following is a Demo of simulating a large screen. If it is a cell phone screen, we are used to completing several activities. Fragment is very useful for porting between the two, and I will post a reference article later.

Go to the program and click Button1. Click Button2. Click Button3 source code:

FragmentDemo.rar



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.