ANDROID
DEVELOPMENT
SESSION 4 – FRAGMENTS
AHMED EZZ EL - DIN
facebook.com/ahmed.e.hassan
1
SESSION CONTENT
•  Fragments
facebook.com/ahmed.e.hassan
2
FRAGMENTS
facebook.com/ahmed.e.hassan
3
A Fragment represents a behavior or a portion of user interface in an Activity.
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).
facebook.com/ahmed.e.hassan
4
FRAGMENTS
A fragment must always be embedded 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.
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.
facebook.com/ahmed.e.hassan
5
FRAGMENTS
However, a fragment is not required to be a part of the activity layout; you may
also use a fragment without its own UI as an invisible worker for the activity.
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.
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.
facebook.com/ahmed.e.hassan
6
FRAGMENTS News Application Example
An example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but
separated for a handset design.
facebook.com/ahmed.e.hassan
7
FRAGMENTS
Creating a Fragment
To create a fragment, you must create a subclass of Fragment (or an existing
subclass of it).
The Fragment class has code that looks a lot like an Activity.
It contains callback methods similar to an activity, such as onCreate(),
onStart(), onPause(), and onStop().
facebook.com/ahmed.e.hassan
8
FRAGMENTS
Usually, you should implement at least the following lifecycle methods:
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize
essential components of the fragment that you want to retain when the fragment is paused or stopped,
then resumed.
onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw
a UI for your fragment, you must return a View from this method that is the root of your fragment's
layout. You can return null if the fragment does not provide a UI.
onPause()
The system calls this method as the first indication that the user is leaving the fragment (though it does
not always mean the fragment is being destroyed). This is usually where you should commit any
changes that should be persisted beyond the current user session (because the user might not come
back).
facebook.com/ahmed.e.hassan
9
facebook.com/ahmed.e.hassan
10
FRAGMENTS
Adding a user interface
A fragment is usually used as part of an activity's user interface and contributes
its own layout to the activity.
To provide a layout for a fragment, you must implement the onCreateView()
callback method, which the Android system calls when it's time for the fragment
to draw its layout. Your implementation of this method must return a View that is
the root of your fragment's layout.
facebook.com/ahmed.e.hassan
11
FRAGMENTS
Adding a user interface
The container parameter passed to onCreateView() is the parent ViewGroup (from the activity's layout)
in which your fragment layout will be inserted.
The savedInstanceState parameter is a Bundle that provides data about the previous instance of the
fragment, if the fragment is being resumed (restoring state is discussed more in the section about
Handling the Fragment Lifecycle).
The inflate() method takes three arguments:
•  The resource ID of the layout you want to inflate.
•  The ViewGroup to be the parent of the inflated layout.
•  A boolean indicating whether the inflated layout should be attached to the ViewGroup.
facebook.com/ahmed.e.hassan
12
FRAGMENTS
Adding a fragment to an activity
Usually, a fragment contributes a portion of UI to the host activity, which is embedded as a part of the
activity's overall view hierarchy. There are two ways you can add a fragment to the activity layout:
Declare the fragment inside the activity's layout file.
facebook.com/ahmed.e.hassan
13
FRAGMENTS
Or, programmatically add the fragment to an existing ViewGroup.
At any time while your activity is running, you can add fragments to your activity layout. You simply
need to specify a ViewGroup in which to place the fragment.
To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must
use APIs from FragmentTransaction.
You can then add a fragment using the add() method, specifying the fragment to add and the view in
which to insert it. For example:

Android development session 4 - Fragments

  • 1.
    ANDROID DEVELOPMENT SESSION 4 –FRAGMENTS AHMED EZZ EL - DIN facebook.com/ahmed.e.hassan 1
  • 2.
  • 3.
    FRAGMENTS facebook.com/ahmed.e.hassan 3 A Fragment representsa behavior or a portion of user interface in an Activity. 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).
  • 4.
    facebook.com/ahmed.e.hassan 4 FRAGMENTS A fragment mustalways be embedded 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. 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.
  • 5.
    facebook.com/ahmed.e.hassan 5 FRAGMENTS However, a fragmentis not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity. 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. 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.
  • 6.
    facebook.com/ahmed.e.hassan 6 FRAGMENTS News ApplicationExample An example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design.
  • 7.
    facebook.com/ahmed.e.hassan 7 FRAGMENTS Creating a Fragment Tocreate a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop().
  • 8.
    facebook.com/ahmed.e.hassan 8 FRAGMENTS Usually, you shouldimplement at least the following lifecycle methods: onCreate() The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed. onCreateView() The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. onPause() The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
  • 9.
  • 10.
    facebook.com/ahmed.e.hassan 10 FRAGMENTS Adding a userinterface A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. To provide a layout for a fragment, you must implement the onCreateView() callback method, which the Android system calls when it's time for the fragment to draw its layout. Your implementation of this method must return a View that is the root of your fragment's layout.
  • 11.
    facebook.com/ahmed.e.hassan 11 FRAGMENTS Adding a userinterface The container parameter passed to onCreateView() is the parent ViewGroup (from the activity's layout) in which your fragment layout will be inserted. The savedInstanceState parameter is a Bundle that provides data about the previous instance of the fragment, if the fragment is being resumed (restoring state is discussed more in the section about Handling the Fragment Lifecycle). The inflate() method takes three arguments: •  The resource ID of the layout you want to inflate. •  The ViewGroup to be the parent of the inflated layout. •  A boolean indicating whether the inflated layout should be attached to the ViewGroup.
  • 12.
    facebook.com/ahmed.e.hassan 12 FRAGMENTS Adding a fragmentto an activity Usually, a fragment contributes a portion of UI to the host activity, which is embedded as a part of the activity's overall view hierarchy. There are two ways you can add a fragment to the activity layout: Declare the fragment inside the activity's layout file.
  • 13.
    facebook.com/ahmed.e.hassan 13 FRAGMENTS Or, programmatically addthe fragment to an existing ViewGroup. At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment. To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example: