Talk about the detailed use of Android fragments
Fragments was born
Since the introduction of the concept of fragments in Android 3.0, it can be translated into fragments and fragments according to the translation of word sea. On it is to solve the dynamic and flexible UI design with different screen resolution. Large screen such as tablet, small screen such as mobile phone, tablet computer design makes it have more space to put more UI components, and the extra space to store UI will produce more interaction, thus giving birth to fragments. The design of fragments does not require you to personally manage the complex changes of view hierarchy. By dispersing the layout of the activity into the frame, you can modify the appearance of the activity at runtime, and save some changes in the back stack managed by the activity.
Fragments design concept
When designing applications, especially Android applications, there are many resolutions to adapt, and fragments allows you to dynamically manage the UI on different screens. For example: communication application (QQ), the user list can be on the left and the message window on the right. Fill in the user list on the mobile phone screen. When a user is clicked, the design of the dialogue window will pop up, as shown in the following figure:
Life cycle of fragments
Each fragment has its own set of lifecycle callback methods and processes its own user input events. Refer to the following figure for the corresponding life cycle
Most of these programs must use fragments. The three callback methods that fragments must implement are:
onCreate
Called when the system creates fragments, which can be used to perform initialization or restore the state when the program is suspended or stopped, which is equivalent to oncreate in activity.
onCreateView
The callback method used to draw the user interface for the first time must return to the fragments view UI to be created. If you don't want to provide fragments user interface, you can return null.
onPause
Called when the user leaves the fragments, you have to submit any changes that should last, because the user may not come back. For more events, please refer to the life cycle diagram above.
Categories of fragments
The system has built-in three kinds of fragments, which have different application scenarios:
DialogFragment
Dialog based fragments, you can add a fragments dialog box to the fragments back stack of activity management, allowing users to return to a previously discarded fragments
ListFragments
It is similar to the effect of listactivity, and also provides functions such as onlistitemclick and setlistadapter.
PreferenceFragments
Similar to preferenceactivity. You can create a setting interface similar to iPad.
Detailed use of fragments
First, let's look at a demo rendering:
When clicking on the left, the characters on the right will be the same as those of the items selected on the left. It is very similar to the setting interface on the iPad. Does this learn from the UI on the iPad?
Related XML files:
Main interface code (commented):
be careful:
1. If you want to create a menu in the fragment, you must set it when oncreate so that it can be created in the optionmenu. The code is:
The subsequent operations can be the same as the normal menu usage of Android. The code is:
Demo download: Android framedemo
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.