What is Fragment?

Fragment is a modular section of an activity, having its own lifecycle, receives its own input events, and which we can add or remove while the activity is running.

Properties of Fragment

  1. must be embedded in an activity i.e. fragment should not exist without activity. When we add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity’s view hierarchy.
  2. fragment lifecycle is directly affected by the host activity lifecycle.
  3. fragment defines its own view layout but we can use a fragment without its own UI as an invisible worker for the activity.
  4. You can insert a fragment into your activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup.
  5. Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets.
  6. We should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.