Android Fragments Training, design modular activities
A piece is a part of an activity that makes it possible to design modular activities. It can be said that the piece is a kind of sub-activity.
important points
- A piece has its own design and behavior and callbacks of its own life cycle.
- When an activity is running, you can add or remove components.
- You can combine several pieces into a single activity to create a multi-level user interface.
- The piece can be used in several activities.
- The life cycle of a part is close to the life cycle of its host activity, ie when the activity stops, all the parts in it also stop.
- A component can perform behavior that has no user interface components.
- Parts in the Honeycomb version of Android, which has the API version 11, have been added to the Android API.
You create fragments by expanding the Fragment class, and you can insert a fragment into your activity pagination by declaring the fragment in the activity pagination file as a <fragment> element.
Before introducing the piece, we had a limit because we could only display a single activity at a time.
So we could not split the device screen and control the different parts separately.
But with the introduction of the piece, we gained more flexibility and removed the restriction of having a single activity on the screen at the same time. We can now have a single activity, but each activity can consist of several pieces that have their own layout, events, and life cycle.
The following example shows how two UI modules can be combined by parts to design a tablet, but these parts must be separated to design a phone.
The program can play two pieces in activity A when running on a tablet-sized device. However, on a screen the size of a phone, there is not enough space for both sections, so activity A contains a snippet that contains the list of articles, and activity B starts when the user selects an article. Which includes the second part for reading the article.
The life cycle of the piece
The life cycle of Android components is very similar to the life cycle of Android activity. This section summarizes the various stages of its life cycle.
Here is a list of methods you can use to skip your piece class –
- onAttach () The sample instance is accompanied by an activity instance. Normally this method is called when the snippet is added to the activity.
- onCreate () The system invokes this method when creating a fragment. You must initialize the basic components of the part you want to keep when pausing or stopping, and then resume.
- onCreateView () The system calls this callback when the component first draws its user interface. To draw the UI for your piece, you must return a View component from this method that is the root of your piece design. If the piece does not provide a UI, you can return null.
- onActivityCreated () When the host activity is created, onActivityCreated () is called after the onCreateView () method. Activity and sample samples as well as activity observation hierarchy are created. In this step, you can access the view with the findViewById () method. Example. In this method you can specify objects that need a context object.
- onStart () The onStart () method is called after the component is visible.
- onResume () The snippet is enabled.
- onPause () The system uses this method as the first indication that the user is leaving the component.
- onstop () stops by calling the piece onStop.
- onDestroyView () The snippet view disappears after this method is called.
- onDestroy () is called at the end of the life of this method.
How to use parts?
The simple steps of creating parts are given below.
- First of all, decide how many pieces you want to use in an activity. For example, we want to use two pieces to manage the horizontal and vertical modes of the device.
- Then create classes that extend the Fragment class based on the number of pieces. The Fragment class has the callback functions mentioned above. You can override any of the functions according to your needs.
- Depending on each piece, you need to create pagination files in the XML file. These files have pagination for defined parts.
- Finally, modify the activity file to define the logic of the replaced parts according to your needs.
Types of parts
Basically, the parts are divided into three classes, which are shown below.
- Single frame fragments – Single frame fragments are used for handheld devices such as mobile phones, here we can only display one piece as a view.
- List fragments – Parts with a special list view are called list fragments.
- Fragments transaction – Using a piece transaction, we can move a piece to another piece.