A fragment represents a behavior or a part of the user interface in an activity. You can combine multiple fragment in an activity to create a multi-panel user interface, and you can reuse the same fragment in multiple activities. You can think of a frament as an activity module, which has its own lifecycle and accepts their own input events, and can be added or deleted when the activity is running (this is like a "sub-activity", which can be reused in different activities ).
Frament must be embedded in an activity, and the lifecycle of fragment is directly affected by this activity. For example, when an activity is terminated, all fragment in the activity will also be terminated. When the activity is destroyed, all fragment in the activity will also be destroyed. However, when an activity is running (that is, when the activity is restored), you can maintain each fragment independently, such as adding or deleting them. When you execute such a fragment transaction, you can also add it to the rollback stack managed by the activity. In the activity, the portal for each rollback stack is a record for a fragment transaction. By pressing the rollback button, the rollback stack allows the user to return a fragment transaction (navigation backward ).
When you add a fragment as part of the activity layout, it lives in the hierarchy tree of the internal activity's viewgroup, and fragment defines its own view layout. You can insert a fragment to your activity by declaring fragment in the activity layout file, orCodeAdd fragment to an existing viewgroup. However, fragment is not necessary as part of the activity layout. You can also use fragment without your own interface to make it an invisible worker of the activity.