1. What is Fragment?
Fragment layout.
Fragment is part of the Activity. We can use multiple Fragment in a single Activity to build a UI Panel, which can be used repeatedly. It can be imagined that it is a module in the Activity. It has its own lifecycle and can receive some user input events. When an Activity is running, it can be added or removed from the Activity, just like viewing Fragment as a child window of the Activity.
Note that a Fragment is embedded in the Activity, and the lifecycle of the Fragment is directly affected by the lifecycle of the main Activity. For example, when an Activity is paused, all Fragment in the Activity will be paused. When the Activity is destroyed, all Fragment will also be destroyed.
When an Activity is running, it can treat every Fragment as an independent one. We can add or remove it. Of course, there will also be a concept of transactions, activity also has the concept of rollback stack. rollback stack is a rollback mechanism based on the BACK key Fragment. Who manages the Fragment rollback stack? It is managed by the Activity. When you execute Fragment, we need to add it to a rollback stack for every execution. Because this Fragment belongs to the Activity, the Activity will record the status information of the rollback stack.
2. Create Fragment
You need to declare a class to inherit Fragment and process some callback methods in Fragment. If you declare a Fragment, you must at least implement the following methods:
1. onCreate ();
It is called when fragment is created. Some variables need to be initialized in this method. This method is generally called only once and will not be called repeatedly. \
2. onCreateView ();
The system will call it to draw the UI control, that is, a View needs to be loaded here. To draw a UI control for Fragment, you need to return a View in this method. It is usually a layout. Return null if the Fragment does not provide the UI.
3. onPause ()
When the system calls it for the first time, it indicates that the user may leave the Fragment, but it does not mean that the Fragment will be destroyed. In fact, it usually needs to submit some changed data here, that is, if the user is about to leave the Fragment, we should submit the data. It is possible that the user will not return after leaving.
3. Add Fragment to Activity
1. In Layout of the Activity, add a LinearLayout, drag the Fragment to the LinearLayout, select the Fragment class name you have prepared, and run the program. (This method does not need to be encoded and is directly implemented using xml.) one of the biggest advantages of using Fragment is that you can design a very complex UI layout.
2. Add a Fragment to a ViewGroup by writing code, that is, add it to Activity.
We can add a Fragment to the Activity layout at any time when the Activity is running. In this way, you need to place a Fragment in the ViewGroup. This method is the most commonly used method in development. To load a Fragment when the Activity is running, such as add, remove, and replace, FragmentManager must be used.
3. FragmentManager
FragmentManager manages fragments. It can add, remove, and replace, that is, remove, add, or replace Fragment. Where can I add Fragment? You can add Fragment to a specified layout or replace it with a layout. This is why you need to specify a layout id in the layout file of the Activity.
4. Why does FragmentTransaction need to be used?
The FragmentTransaction object is used in the Activity to change the influence on the layout by calling the commit () method of fragmentTransation after adding, remove, and replace a Fragment in Acivity.