Static loading and dynamic loading examples of fragment in Android Application Development
1. The static use of fragment is a part of the UI of the activity. It is embedded in the activity. Multiple fragments can divide an activity into multiple parts, which will be used more in large screen mobile phones or tablets, so it doesn't need to use multiple activities to switch. Of course, fragment can not be displayed and only process some data in the background. This article will not talk about this for the time being. Here's how to statically add fragment. XML to the activity's layout file
Custom fragments usually inherit the fragment class, and some special ones inherit listfragment, dialogfragment, etc. Three methods are usually implemented to inherit the fragment class: oncreate(), oncreateview(), onpause();
I have defined two fragments in the activity, one is the leftfragment on the left and the other is the rightfragment on the right. The following is the code: first, we need to implement our own fragment class
Leftfragment class:
Several callback functions are implemented here, mainly to see the relationship between the life cycle of activity and fragment. Oncreateview() method returns the layout corresponding to this fragment to the layout of activity and lets the activity load it. The first parameter r.layout.leftfragment in the inflate.inflate (r.layout.leftfragment, true) method is the layout file ID corresponding to this fragment, The second parameter container is the target activity to be inserted. The third parameter determines whether the fragment is attached to the layout file corresponding to the container. Leftfragment:
Rightfragment class: similar to leftfragment
Layout file corresponding to rightfragment:
Finally, the activity layout file:
Add the fragment tag to the layout file in the activity, where the Android: name attribute corresponds to the full name of the custom fragment class. The system will get the layout of the fragment according to this call to the oncreateview() method of the specified fragment, and then add it to the activity. The container parameter in the oncreateview() method is passed in the past at this time. Look at the display results:
When you open a program, the lifecycle displays:
When you press the back key, the lifecycle displays:
2. Use fragment dynamically
The simplest way to use fragments has been demonstrated above. Let's share how to dynamically add, update and delete fragments.
First, the layout file activity of mainactivity_ Main.xml, the top of the layout file is a titlefragment, which is a statically declared fragment.
There is also a fragment in the middle, but this fragment is used dynamically.
At the bottom are four buttons. Use the include tag to include external layout files.
Then there is the mainactivity. Java file. It is also the most important code file in our demo. First, load the above layout file through setcontentview(), and then through setdefaultfragment(); Load the default contentfragment dynamically. The next step is to dynamically switch fragments at will through the four buttons we prevent at the bottom. That's why fragment is so popular~~~^^
From the above code, we can see that we can use fragmentmanager to dynamically load fragments. The replace method used here ~ ~ ~ in the next section, we will introduce the common APIs of fragmentmanager in detail^^
Note: if you use Android version 3.0 or later, you need to import the V4 package, and then the activity inherits the fragmentactivity, and then obtains the fragmentmanager object through getsupportfragmentmanager(). However, it is recommended to change the minsdkversion and targetsdkversion of the uses SDK of the menifest file to above 11, so you don't need to import the V4 package.
There are two dynamically loaded fragments in the middle of the code. This is the same as the declaration method of statically using fragments. Write a class that inherits fragments, and then set the corresponding layout. Due to the relationship of time, I only wrote two fragments here. Now post the code pages of these two fragments:
The first fragment and its corresponding layout file:
The second fragment and its corresponding layout file:
Well, now that we have the basic code, we'll post the operation diagram of the demo for everyone to share (Note: due to time, we didn't pay attention to the layout and picture beautification, but the function implementation). This is the effect diagram of clicking the first and second buttons below respectively, so as to dynamically load the two fragments with a fragment in the middle.
PS: in order to simplify the code, we don't add button click changes. We mainly explain the functions~~~