Fragment: fragment and fragment, which are lightweight and flexible. fragment is a technology required to develop Android tablets. It blocks the UI and organizes the UI in blocks to achieve reuse of the UI components.
1. It represents an action or a part of a user interface in an activity.
We can combine different fragments into an activity, or reuse a fragment in different activities. You can regard a fragment as a clip in an activity. It has its own lifecycle (how to create and destroy it) and accept its own input events, you can dynamically add or remove a fragment when the activity is running.
2. A fragment must always be bound to an activity. Although fragment has its own lifecycle, it is also directly affected by the lifecycle of its host activity.
For example, when an activity is paused, all fragments in the activity are also paused. When the activity is destroyed, all fragment in the activity is also destroyed. However, when an activity is running (when it is in the resumed lifecycle), we can independently manipulate each fragment to execute injection, add, and remove them. In addition, when you execute a fragment operation, you can add it to a history stack, which is maintained by its host activity. The elements in each stack Are fragment that has been executed. This stack allows us to use the return key to restore the previous fragment.
3. fragment can define its own layout file (layout). Of course, fragment does not have its own UI.
When you add a fragment as part of the layout file of the activity, it is in the viewgroup in the view level of the activity. (1) You can define the fragment of a class by defining the <fragment> element in layout of the activity. (2) Add it to the current viewgroup (view combination, or view element combination) in the code. Note: A fragment is not necessarily a part of the activity layout file. We can also use a fragment without its own UI as an invisible part of the activity.
Note: fragment is the technology that starts with android3.0, and Google provides android-support-v4.jar packages to be compatible with Versions later than 1.6. You can use SDK manager to download the androidsupport package for Android support under the extras directory. During use, you need to create the libs folder on the top layer of the project code and copy the jar library file to libs. The following uses the jar to study fragment.
4. Fragment object lifecycle.
1. Create the layout file layout1
[Java]View plaincopy
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: layout_gravity = "center"
- Android: gravity = "center_vertical"
- Android: Orientation = "vertical">
- <Textview
- Android: Id = "@ + ID/textview1"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "My first fragment for android1.6"
- Android: textcolor = "# ff00ff"
- Android: textsize = "33sp"/>
- </Linearlayout>
2. Create the activity_main layout file.
[Java]View plaincopy
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Xmlns: Tools = "http://schemas.android.com/tools"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: gravity = "center"
- Android: Orientation = "vertical">
- <Textview
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: layout_centerhorizontal = "true"
- Android: layout_centervertical = "true"
- Android: text = "@ string/hello_world"
- Tools: context = ". mainactivity"/>
- <Fragment
- Android: Name = "com. example. myfragment. myfragment1"
- Android: Id = "@ + ID/myfragment1"
- Android: Tag = "myfragment"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"/>
- </Linearlayout>
3. Create myfragment1
[Java]View plaincopy
- Package com. example. myfragment;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. Support. v4.app. Fragment;
- Import Android. util. log;
- Import Android. View. layoutinflater;
- Import Android. View. view;
- Import Android. View. viewgroup;
- Public class myfragment1 extends fragment {
- @ Override
- Public void onattach (activity ){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onattach ");
- Super. onattach (activity );
- }
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "oncreate ");
- Super. oncreate (savedinstancestate );
- }
- @ Override
- Public View oncreateview (layoutinflater Inflater, viewgroup container,
- Bundle savedinstancestate ){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "oncreateview ");
- Return Inflater. Inflate (R. layout. layout1, container, false );
- }
- @ Override
- Public void onactivitycreated (bundle savedinstancestate ){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onactivitycreated ");
- Super. onactivitycreated (savedinstancestate );
- }
- @ Override
- Public void onstart (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onstart ");
- Super. onstart ();
- }
- @ Override
- Public void onresume (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onresume ");
- Super. onresume ();
- }
- @ Override
- Public void onpause (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onpause ");
- Super. onpause ();
- }
- @ Override
- Public void onstop (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "onstop ");
- Super. onstop ();
- }
- @ Override
- Public void ondestroyview (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "ondestroyview ");
- Super. ondestroyview ();
- }
- @ Override
- Public void ondestroy (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "ondestroy ");
- Super. ondestroy ();
- }
- @ Override
- Public void ondetach (){
- // Todo auto-generated method stub
- Log. D (this. gettag (), "ondetach ");
- Super. ondetach ();
- }
- }
4. Create a mainactivity File
[Java]View plaincopy
- Package com. example. myfragment;
- Import Android. OS. Bundle;
- Import Android. Support. v4.app. fragmentactivity;
- Public class mainactivity extends fragmentactivity {
- // Here it inherits frameactivity, not activity
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. activity_main );
- }
- }
Run the command, press the Home Key, enter the program, and then press the back key. The effect is as follows:
Note:
1. Reference Android. Support. v4.app. *; components in the package;
2. To be compatible with android1.6, mainactivity inherits from frameactivity;
3. In the modified Code, fragment has been written into XML. The disadvantage is that it cannot be dynamically added or deleted. later learning will use code to add or delete fragment.
4. Inflater. Inflate (R. layout.Layout1, Container,False);
The first parameter converts a layout file to a view object;
The second parameter is to put the view object into the container. The Container is actually the root node of activity_main. You can set the tag attribute under the root node and use the container. get the value of gettag () and print it out.
The third parameter indicates whether to add the view object to the container. in XML, we have added it to the container. After testing, if you set it to true again, it can still be correctly executed.
5. Fragment Lifecycle Analysis
(1) onattach () // called when a fragment object is associated with an activity.
(2) oncreate () // called when the fragment object is initially created.
(3) oncreateview () // call when creating a view associated with the fragment object.
(4) onactivitycreated () // called when the activity object completes its oncreate method.
(5), onstart () // fragment object called when the UI is visible.
(6) The onresume () // fragment object UI can be called when it interacts with the user.
(7), onpause () // fragment object visible, but not interactive. Called when an activity object is converted to the onpause status.
(8), onstop () // space completely blocked; or called when the host activity object is changed to onstop status.
(9), ondestroyview () // called when the fragment object clears the view resource, that is, the view in fragment is removed.
(10), ondestroy () // call the fragment object to clear the view resource.
(11), ondetach () // call when the fragment object is not associated with the activity object.
Execute 1-4 when a fragment is created;
When this fragment is created and visible to users, execute 5-6;
When this fragment is visible to the invisible, execute 7-8;
When this fragment is destroyed (or the activity holding it is destroyed), run 9-11;
In addition, in 3-5, you can use the bundle object to save a fragment object.
Fragment in-depth understanding (2)