Fragment relies on activity, but is independent of activity, it can be regarded as the child activity inside activity, generally used in flat, because one activity space is too large, so it can be divided into several fragments to use, So why is his name Fragment. Fragment is also necessary for software porting of mobile phones and tablets.
There are two ways to use fragment, one is to write dead in XML, and one is to load Java code. The steps to use are: 1, define a class that inherits fragment, and the corresponding XML layout file, 2, override several necessary methods OnCreate (), Oncreateview (), OnPause () in the inheriting class, and complete the required operation; 3. Load the Fragment in one of two ways in the main activity or other Fragment .
the life cycle of the fragment is as follows:
After the red is loaded, the following red is exited after exiting the current fragment or activity.
The following is the simulation of large screen demo, if it is a mobile phone small screen, we are accustomed to use a few activity to complete, fragment to the transplant between the two is very useful, and then post a reference article.
Enter the program click Button1 Click Button2 click Button3 Enter the password randomly
Demo Frame: 1, the main activity does not do anything, the layout file has a fragment (the first way to load, write dead in the XML), and a linear layout (used to store the back of the fragment); 2, Define a class that inherits fragment, and an XML layout file with three buttons, which is the left piece, which overrides several methods, where the Oncreateview () method is used to generate the view. 3, the definition of three inherited fragment class, and the corresponding layout file, layout content can be seen; 4, the button in the fragment in 2 to set the Listener, click on each button to load different fragment (the second mode of loading); 5, in 4 Fragment to complete a specific operation.
Demo uses more than 3.0 version, if you use the following 3.0, refer to the article:
Http://www.cnblogs.com/mengdd/archive/2013/01/08/2851368.html
For fragment porting features for tablets and mobile phones, refer to the article:
Http://www.2cto.com/kf/201305/210818.html
Source:
Fragmentdemo.rar
Android Components: Fragment instances