SlideShare a Scribd company logo
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:

More Related Content

PPTX
PDF
[Individual presentation] android fragment
PPT
Introduction to Android Fragments
PDF
Android Fragment
PDF
Short Intro to Android Fragments
PDF
Android - Working with Fragments
PDF
Introduction to fragments in android
ODP
Android App Development - 06 Fragments
[Individual presentation] android fragment
Introduction to Android Fragments
Android Fragment
Short Intro to Android Fragments
Android - Working with Fragments
Introduction to fragments in android
Android App Development - 06 Fragments

What's hot (20)

PDF
Fragments: Why, How, What For?
PPTX
Explanation onAttach() of Fragment class in Android
PPTX
Activities, Fragments, and Events
PDF
Android Components
PDF
Fragments anyone
ODP
Android App Development - 02 Activity and intent
PPT
android activity
PDF
Android development - Activities, Views & Intents
PPTX
Presentation on Android application life cycle and saved instancestate
PDF
Managing Activity Backstack
PDF
Android UI Fundamentals part 1
PDF
Android Basic Components
PPTX
Android development session 2 - intent and activity
PPTX
Android
ODP
Ppt 2 android_basics
PPTX
Android apps development
PPTX
04 activities - Android
PDF
Intents are Awesome
DOC
Day 4: Activity lifecycle
PPTX
Android activity lifecycle
Fragments: Why, How, What For?
Explanation onAttach() of Fragment class in Android
Activities, Fragments, and Events
Android Components
Fragments anyone
Android App Development - 02 Activity and intent
android activity
Android development - Activities, Views & Intents
Presentation on Android application life cycle and saved instancestate
Managing Activity Backstack
Android UI Fundamentals part 1
Android Basic Components
Android development session 2 - intent and activity
Android
Ppt 2 android_basics
Android apps development
04 activities - Android
Intents are Awesome
Day 4: Activity lifecycle
Android activity lifecycle
Ad

Viewers also liked (20)

PDF
Android Udacity Study group 1
PDF
Workshop on Search Engine Optimization
PPTX
Optimizing apps for better performance extended
PPTX
Optimizing Apps for Better Performance
PDF
App indexing api
PDF
Workhsop on Logic Building for Programming
PDF
Project Analysis - How to Start Project Develoment
PPTX
Hack'n Break Android Workshop
PDF
Lecture 04. Mobile App Design
PPTX
Android development session 3 - layout
PPTX
Overview of DroidCon UK 2015
PPTX
What's new in Android at I/O'16
PDF
Fundamental of android
PDF
Working better together designers &amp; developers
PPTX
Management Innovation
PPTX
Workshop Android for Java Developers
PPTX
Session #8 adding magic to your app
PDF
Lecture 06. iOS Programming. Основи Objective-C
PDF
Android Development Workshop
PDF
Workshop on How to crack interview
Android Udacity Study group 1
Workshop on Search Engine Optimization
Optimizing apps for better performance extended
Optimizing Apps for Better Performance
App indexing api
Workhsop on Logic Building for Programming
Project Analysis - How to Start Project Develoment
Hack'n Break Android Workshop
Lecture 04. Mobile App Design
Android development session 3 - layout
Overview of DroidCon UK 2015
What's new in Android at I/O'16
Fundamental of android
Working better together designers &amp; developers
Management Innovation
Workshop Android for Java Developers
Session #8 adding magic to your app
Lecture 06. iOS Programming. Основи Objective-C
Android Development Workshop
Workshop on How to crack interview
Ad

Similar to Android development session 4 - Fragments (20)

PPTX
Tk2323 lecture 6 fragment (new)
PDF
Fragments In Android
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
PPTX
fragments-activity.pptx
PPTX
深入淺出談Fragment
DOCX
Introduction to Android Fragment and its Lifecycle.docx
PPTX
Lesson 4
PPTX
Fragmentation in android
PPTX
Android Fragments
PPTX
Unit-1.2 Android-Activities, Fragments, and Intents (1).pptx
PPTX
learn about Android Extended and Fragments.pptx
PPTX
Android apps development
DOCX
Activity
DOCX
Activity
DOCX
Activity
DOCX
Activity
PPTX
02. Android application development_Lec2.pptx
PPT
PPTX
What the fragments
PDF
Lab3-Android
Tk2323 lecture 6 fragment (new)
Fragments In Android
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
fragments-activity.pptx
深入淺出談Fragment
Introduction to Android Fragment and its Lifecycle.docx
Lesson 4
Fragmentation in android
Android Fragments
Unit-1.2 Android-Activities, Fragments, and Intents (1).pptx
learn about Android Extended and Fragments.pptx
Android apps development
Activity
Activity
Activity
Activity
02. Android application development_Lec2.pptx
What the fragments
Lab3-Android

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PPTX
Lesson notes of climatology university.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Insiders guide to clinical Medicine.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
102 student loan defaulters named and shamed – Is someone you know on the list?
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Insiders guide to clinical Medicine.pdf
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study

Android development session 4 - Fragments

  • 1. ANDROID DEVELOPMENT SESSION 4 – FRAGMENTS AHMED EZZ EL - DIN facebook.com/ahmed.e.hassan 1
  • 3. 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).
  • 4. 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.
  • 5. 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.
  • 6. 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.
  • 7. 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().
  • 8. 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).
  • 10. 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.
  • 11. 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.
  • 12. 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.
  • 13. 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: