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