Real complete parsing of fragment in Android (Part 1)

Since fragment appeared, it's been a while. I feel that we can talk about everything with fragment. We have to ask whether fragment can be realized ~ ~ ~ ha ha, isn't it a little too much~~~

This paper tries to explain how fragment is generated, what is fragment, fragment life cycle, how to use fragment statically and dynamically, fragment fallback stack and fragment transaction; And some special uses of fragment, such as: what is the use of fragment without layout? How does fragment interact with activity? How does fragment create a dialog box? How fragment integrates with actionbar, etc.

1. Generation and introduction of fragment

Android runs on a variety of devices, including small screen mobile phones, large screen tablets and even televisions. In view of the gap in screen size, in many cases, we first develop a set of app for mobile phones, then copy a copy, and modify the layout to adapt to the super large screen of flat-panel Shenma. Can't an app adapt to both mobile phones and tablets? Of course, it must be. Fragment appeared to solve this problem. You can regard fragment as an integral part of an activity interface, and even the activity interface can be completely composed of different fragments. What's more handsome is that fragment has its own life cycle and receives and processes user events, so it's not necessary to write a pile of event processing code of controls in the activity. More importantly, you can dynamically add, replace and remove a fragment.

2. Fragment lifecycle

Fragments must exist depending on activities, so the life cycle of activities will directly affect the life cycle of fragments. The figure on the official website well illustrates the relationship between the two life cycles:

You can see that fragment has several additional lifecycle callback methods than activity:

Called when a fragment is associated with an activity.

Create a view of the fragment

Called when the oncreate method of the activity returns

Corresponding to oncreateview, called when the view of the fragment is removed

Corresponding to onattach, called when the association between fragment and activity is cancelled

Note: if you override all other methods except oncreateview, you must call the implementation of the method by the parent class,

3. Use fragment statically

Hey, hey, it's time to use it at last~~

This is the simplest way to use fragment. Take fragment as an ordinary control and write it directly in the layout file of activity. Steps:

1. Inherit fragment and override oncreateview to determine the layout of fragemnt

2. Declare this fragment in the activity as if it is the same as an ordinary view

An example is shown below (I use two fragments as the layout of the activity, one for the title layout and one for the content layout):

Layout file of titlefragment:

TitleFragment

Similarly, there is also the layout file of contentfragment:

MainActivity

Layout file of activity:

Is the fragment declared in the activity layout file like an ordinary view, and then the event processing and other codes of all controls are processed by their own fragments, which makes the activity feel clean and woody ~ ~ does the readability, reusability and maintainability of the code improve instantly ~ ~ ~ let's see the effect picture below:

4. Dynamic use of fragment

The simplest way to use fragments has been demonstrated above ~ the following describes how to dynamically add, update, and delete fragments

In order to use fragment dynamically, we modify the layout file of actvity, use a FrameLayout in the middle, and add four buttons ~ ~ ~ hey ~ ~ not wechat --!

The layout of the four buttons at the bottom will not be pasted. You will understand it by looking at the renderings~~

Main activity below

You can see that we use fragmentmanager to dynamically load fragments. Here we use the replace method ~ ~ in the next section, I will introduce the common APIs of fragmentmanager in detail.

Note: if you use Android version 3.0 or below, you need to import the V4 package, then the activity inherits the fragmentactivity, and then obtain the fragmentmanager through getsupportfragmentmanager. However, it is recommended that the minsdkversion and targetsdkversion of the uses SDK of the menifest version be changed to above 11, so there is no need to introduce the V4 package.

There are also two subclasses of fragment in the code. Contentfragment has been seen above, and friendfragment is actually similar:

design sketch:

5. Common APIs of fragment family

Fragment has three common classes:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>