SlideShare a Scribd company logo
1 of 9
Fragments
PREPARED BY: SUDHANSHU VOHRA
What are Fragments ?
 Fragments are Android App components that
represent a self contained portion of UI in an Activity.
 Fragments are very similar to Activity’s and they can
be referred to as mini-Activities.
 The fragments are composed of a layout
file(determines the view of the fragment) and the java
file(brain of the fragment code).
 One or more fragments can be added into a single
Activity.
Fragment v/s Activity
 Fragments have their own lifecycle, they receive their
own set of inputs and can be easily added or removed
from an activity at runtime.
 Fragments are always embedded in Activities and their
lifecycle is directly affected by the life cycle of the host
activity.
Why are Fragments used ?
 Fragments brings in two
important features i.e. they
modularize our app and they
are reusable components which
makes they widely used.
 They can be added or removed
any time during the runtime of
the app.
Master Detail Flow
How to create Fragments ?
How are fragments added and removed
from activities ?
 FragmentManager is the class or component which is
used to add or remove fragments for activities. It is
used to execute transactions on Fragments. There are
some basic type of transactions:
1. Add- Used to add a fragment in place of a container
view.
2. Replace- Used to replace any existing fragment from
the existing view.
Syntax:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); //
newInstance() is a static factory method.
transaction.commit();
When to use fragments ?
 Deciding between using activities and fragments is the problem with
most new Android programmers:
1. Reusability: Think of using fragments whenever you need some
reusable components (UI + Java Code = UX). Who likes rewriting code
?
2. Modular: Fragments code is cleanly separated from the parent
activities code so using them is easy as they as bring in the code
property of modularity.
3. Changing UI instantly and data passing is also easier.
4. When coding for a variety of devices like mobiles, tablets, smart
watches etc. then using fragments is always a safe bet as their look
and feel can be easily changed.
Any Questions ?
Thank You
 Thanks for being a great audience and for your
valuable time.

More Related Content

What's hot

What's hot (10)

[Individual presentation] android fragment
[Individual presentation] android fragment[Individual presentation] android fragment
[Individual presentation] android fragment
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
 
Dagger2
Dagger2Dagger2
Dagger2
 
Smash your Android App into Fragments
Smash your Android App into FragmentsSmash your Android App into Fragments
Smash your Android App into Fragments
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
 
Android Dagger2
Android Dagger2Android Dagger2
Android Dagger2
 
Dagger in multi module sdk
Dagger in multi module sdkDagger in multi module sdk
Dagger in multi module sdk
 
android activity
android activityandroid activity
android activity
 
Objective c design pattens-architetcure
Objective c design pattens-architetcureObjective c design pattens-architetcure
Objective c design pattens-architetcure
 
Lecture #4 activities & fragments
Lecture #4  activities & fragmentsLecture #4  activities & fragments
Lecture #4 activities & fragments
 

Similar to Fragments

Fragments In Android
Fragments In AndroidFragments In Android
Fragments In AndroidDivyaKS12
 
Design Pattern with Actionscript
Design Pattern with ActionscriptDesign Pattern with Actionscript
Design Pattern with ActionscriptDaniel Swid
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Flutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdfFlutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdfUtibe5
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 FragmentsDiego Grancini
 
Dori waldman android _course
Dori waldman android _courseDori waldman android _course
Dori waldman android _courseDori Waldman
 
Deep diving into building lightning components
Deep diving into building lightning componentsDeep diving into building lightning components
Deep diving into building lightning componentsCloud Analogy
 
[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Android[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Androidrizki adam kurniawan
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKnoldus Inc.
 
ActionScript Design Patterns
ActionScript Design Patterns ActionScript Design Patterns
ActionScript Design Patterns Yoss Cohen
 
Java remote control for laboratory monitoring
Java remote control for laboratory monitoringJava remote control for laboratory monitoring
Java remote control for laboratory monitoringIAEME Publication
 

Similar to Fragments (20)

Fragment debugging
Fragment debuggingFragment debugging
Fragment debugging
 
Fragments In Android
Fragments In AndroidFragments In Android
Fragments In Android
 
Design Pattern with Actionscript
Design Pattern with ActionscriptDesign Pattern with Actionscript
Design Pattern with Actionscript
 
Fragments In Android.pptx
Fragments In Android.pptxFragments In Android.pptx
Fragments In Android.pptx
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Spring boot
Spring bootSpring boot
Spring boot
 
Android
AndroidAndroid
Android
 
PureMVC
PureMVCPureMVC
PureMVC
 
Flutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdfFlutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdf
 
Android beginners David
Android beginners DavidAndroid beginners David
Android beginners David
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
 
Angular2 and You
Angular2 and YouAngular2 and You
Angular2 and You
 
Dori waldman android _course
Dori waldman android _courseDori waldman android _course
Dori waldman android _course
 
Deep diving into building lightning components
Deep diving into building lightning componentsDeep diving into building lightning components
Deep diving into building lightning components
 
[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Android[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Android
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development Presentation
 
Android os
Android osAndroid os
Android os
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
ActionScript Design Patterns
ActionScript Design Patterns ActionScript Design Patterns
ActionScript Design Patterns
 
Java remote control for laboratory monitoring
Java remote control for laboratory monitoringJava remote control for laboratory monitoring
Java remote control for laboratory monitoring
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 

Fragments

  • 2. What are Fragments ?  Fragments are Android App components that represent a self contained portion of UI in an Activity.  Fragments are very similar to Activity’s and they can be referred to as mini-Activities.  The fragments are composed of a layout file(determines the view of the fragment) and the java file(brain of the fragment code).  One or more fragments can be added into a single Activity.
  • 3. Fragment v/s Activity  Fragments have their own lifecycle, they receive their own set of inputs and can be easily added or removed from an activity at runtime.  Fragments are always embedded in Activities and their lifecycle is directly affected by the life cycle of the host activity.
  • 4. Why are Fragments used ?  Fragments brings in two important features i.e. they modularize our app and they are reusable components which makes they widely used.  They can be added or removed any time during the runtime of the app. Master Detail Flow
  • 5. How to create Fragments ?
  • 6. How are fragments added and removed from activities ?  FragmentManager is the class or component which is used to add or remove fragments for activities. It is used to execute transactions on Fragments. There are some basic type of transactions: 1. Add- Used to add a fragment in place of a container view. 2. Replace- Used to replace any existing fragment from the existing view. Syntax: FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method. transaction.commit();
  • 7. When to use fragments ?  Deciding between using activities and fragments is the problem with most new Android programmers: 1. Reusability: Think of using fragments whenever you need some reusable components (UI + Java Code = UX). Who likes rewriting code ? 2. Modular: Fragments code is cleanly separated from the parent activities code so using them is easy as they as bring in the code property of modularity. 3. Changing UI instantly and data passing is also easier. 4. When coding for a variety of devices like mobiles, tablets, smart watches etc. then using fragments is always a safe bet as their look and feel can be easily changed.
  • 9. Thank You  Thanks for being a great audience and for your valuable time.