Week#10 Fragments
Week#10 Fragments
What is a Fragment
A Fragment represents a behavior or a portion of user interface in a FragmentActivity.
You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a
fragment in multiple activities.
You can think of a fragment as a modular section of an activity, which has its own lifecycle,
receives its own input events, and which you can add or remove while the activity is running
(sort of like a "sub activity" that you can reuse in different activities).
Need of Fragments
Before the introduction of Fragment’s we can only show a single Activity on the screen at one
given point of time so we were not able to divide the screen and control different parts
separately. With the help of Fragment’s we can divide the screens in different parts and controls
different parts separately.
By using Fragments we can comprise multiple Fragments in a single Activity. Fragments have
their own events, layouts and complete life cycle. It provide flexibility and also removed the
limitation of single Activity on the screen at a time.
Purpose of Fragments
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.
Because a tablet's screen is much larger than that of a handset, there's more room to combine and
interchange UI components.
Fragments allow such designs without the need for you to manage complex changes to the view
hierarchy.
By dividing the layout of an activity into fragments, you become able to modify the activity's
appearance at runtime and preserve those changes in a back stack that's managed by the activity.
Example
For example, a news application can use one fragment to show a list of articles on the left and
another fragment to display an article on the right
—both fragments appear in one activity, side by side, and each fragment has its own set of
lifecycle callback methods and handle their own user input events.
Behavior of a Fragment
A fragment must always be hosted in an activity and the fragment's lifecycle is directly affected
by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in
it, and when the activity is destroyed, so are all fragments.
However, while an activity is running (it is in the resumed lifecycle state), you can manipulate
each fragment independently, such as add or remove them.
When you perform such a fragment transaction, you can also add it to a back stack that's
managed by the activity—each back stack entry in the activity is a record of the fragment
transaction that occurred.
The back stack allows the user to reverse a fragment transaction (navigate backwards), by
pressing the Back button.
How to add a Fragment
When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the
activity's view hierarchy and the fragment defines its own view layout.
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.
See FragEx1 and listen to FragEx1 video file
Adding fragments dynamically
See FragEx2 project and listen to FragEx2 video file