Why introduce a fragment? In order to adapt to the needs of users or devices, the activity interface can be assembled and even reassembled at runtime. The activity itself does not have this flexibility. The activity view can be switched at runtime, but the code that controls the view must be implemented in the activity. As a result, each activity still has to be tightly bound to a specific user interface.
Using fragment instead of activity to manage the application UI can bypass the limitations of the Android system activity usage rules. A fragment is a controller object that an activity can delegate to perform a task. These tasks are usually the administrative user interface. The managed user interface can be a full screen or part of a full screen. The fragment that manages the user interface is also known as the UI fragment. It also has its own view that produces the layout file. The fragment view contains visual UI elements that the user can interact with. The activity view can reserve a location for the fragment view to be inserted. According to the application and user needs, you can use the fragment and activity to assemble or reorganize the user interface.
First, design a UI fragment called CrimeFragment to manage the user interface shown in Figure 7-4, and then design an activity called CrimeActivity to host the CrimeFragment instance. Hosting can be understood in this way: the activity provides a location in its view hierarchy for placing the fragment view, as shown in Figure 7-5. The fragment itself does not have the ability to display views on the screen. Therefore, the fragment view can only be displayed on the screen by placing its view in the view hierarchy of the activity.