SlideShare a Scribd company logo
1 of 12
https://marif.yolasite.com/resources
ACTIVITY AND
FRAGMENTS
Application Components - Activity
• An activity represents a single screen with a user interface, in-short Activity performs actions on the
screen.
• If an application has more than one activity, then one of them should be marked as the activity that is
presented when the application is launched.
Activity Lifecycle
• An activity has essentially four states:
• If an activity in the foreground of the screen (at the top of the stack), it is active or running.
• If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity
has focus on top of your activity), it is paused. A paused activity is completely alive.
• If an activity is completely obscured by another activity, it is stopped. It still retains all state and
member information, however, it is no longer visible to the user so its window is hidden and it will
often be killed by the system when memory is needed elsewhere.
• If an activity is paused or stopped, the system can drop the activity from memory by either asking
it to finish, or simply killing its process. When it is displayed again to the user, it must be
completely restarted.
Application Components - Activity
• There are three key loops you may be
interested in monitoring within your activity:
• The entire lifetime of an activity happens
between the first call to onCreate(Bundle)
through to a single final call to onDestroy().
• The visible lifetime of an activity happens
between a call to onStart() until a
corresponding call to onStop().
• The foreground lifetime of an activity
happens between a call to onResume() until
a corresponding call to onPause().
Main Building Blocks – Fragments
• Fragment represents a behavior or a portion of UI in an Activity. It is a kind of sub-activity.
• Multiple fragments can be combined in a single activity to build a multi-pane UI and reuse a fragment
in multiple activities.
• You can add or remove fragments in an activity while activity is running.
• An activity can contain any number of fragments.
• Fragment life cycle is closely related to lifecycle of its host activity which means when activity is
paused, all fragments available in activity will also be stopped.
• Fragments were added to Android API in Honeycomb(3.0) version of Android which API version 11.
• Earlier we had a limitation because we can show only a single activity on screen at one given point in
time. So we were not able to divide device screen and control different parts separately.
• But with fragment we got more flexibility and removed limitation of having a single activity on screen at
a time. Fragments will have their own layout, events and complete lifecycle.
• You create fragments by extending Fragment class and you can insert a fragment into your activity
layout by declaring fragment in activity's layout file, as a <fragment> element.
Main Building Blocks – Fragments
Main Building Blocks – Fragment Lifecycle
Phase I: When a fragment gets created, it goes through following states:
• onAttach() // when a frag is attached to its hosting activity
• onCreate() //frag is initialized, but no UI
• onCreateView() //frag sets up and returns its UI. This view is given to
hosting activity afterwards.
• onActivityCreated() // Now frag’s life cycle is depending upon its hosting
activity’s life cycle
Phase II: When fragment becomes visible, it goes through these states:
• onStart() //hosting activity is about to become visible
• onResume() // hosting activity is about to become visible and
ready for user interaction
Phase III: When fragment goes into background mode, it goes through this
states.
• onPaused() //hosting activity is visible, but another activity is in
the foreground and has focus
• onStop() // When hosting activity is not visible
Main Building Blocks – Fragment Lifecycle
Phase IV: When fragment is destroyed, it goes through following states:
• onPaused() • onStop()
• onDestroyView() //hosting activity is about to be destroyed any frag
that it is hosting also has to be shut down
• onDestroy() //release frag resources
• onDetach() //null out references to hosting activity
Adding Fragments to Activities
Two general ways
1. First,
i. Fragment can be statically added to the activity’s layout file.
ii. It is then used in a call to setContentView method.
2. Second,
i. Add it programmatically using FragmentManager
Main Building Blocks – How to use Fragments?
This involves number of simple steps to create Fragments.
• First of all decide how many fragments you want to use in an activity. For example let's we want to
use two fragments to handle landscape and portrait modes of the device.
• Next based on number of fragments, create classes which will extend the Fragment class. Fragment
class has above mentioned callback functions. You can override any of the functions based on your
requirements.
• Corresponding to each fragment, you will need to create layout files in XML file. These files will have
layout for the defined fragments.
• Finally modify activity file to define actual logic of replacing fragments based on your requirement.
A Useful Trick!
• Press Ctrl and click on keyword of java in Android Studio. You will get complete definitions of that
keyword.
fragments-activity.pptx
fragments-activity.pptx
fragments-activity.pptx

More Related Content

Similar to fragments-activity.pptx

Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Gabor Varadi
 
Dori waldman android _course_2
Dori waldman android _course_2Dori waldman android _course_2
Dori waldman android _course_2Dori Waldman
 
Android activity
Android activityAndroid activity
Android activityKrazy Koder
 
Android activity
Android activityAndroid activity
Android activityKrazy Koder
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android applicationNikunj Dhameliya
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116PrathishGM
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8Utkarsh Mankad
 
Short Intro to Android Fragments
Short Intro to Android FragmentsShort Intro to Android Fragments
Short Intro to Android FragmentsJussi Pohjolainen
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestateOsahon Gino Ediagbonya
 
Android activity
Android activityAndroid activity
Android activityMohNage7
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2DHIRAJ PRAVIN
 
Android life cycle
Android life cycleAndroid life cycle
Android life cycle瑋琮 林
 

Similar to fragments-activity.pptx (20)

Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
Dori waldman android _course_2
Dori waldman android _course_2Dori waldman android _course_2
Dori waldman android _course_2
 
Android activity
Android activityAndroid activity
Android activity
 
Android activity
Android activityAndroid activity
Android activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android application
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
Short Intro to Android Fragments
Short Intro to Android FragmentsShort Intro to Android Fragments
Short Intro to Android Fragments
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
Android activity
Android activityAndroid activity
Android activity
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2
 
Android life cycle
Android life cycleAndroid life cycle
Android life cycle
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 

Recently uploaded

Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 

Recently uploaded (20)

Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 

fragments-activity.pptx

  • 2. Application Components - Activity • An activity represents a single screen with a user interface, in-short Activity performs actions on the screen. • If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched. Activity Lifecycle • An activity has essentially four states: • If an activity in the foreground of the screen (at the top of the stack), it is active or running. • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive. • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted.
  • 3. Application Components - Activity • There are three key loops you may be interested in monitoring within your activity: • The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause().
  • 4.
  • 5. Main Building Blocks – Fragments • Fragment represents a behavior or a portion of UI in an Activity. It is a kind of sub-activity. • Multiple fragments can be combined in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. • You can add or remove fragments in an activity while activity is running. • An activity can contain any number of fragments. • Fragment life cycle is closely related to lifecycle of its host activity which means when activity is paused, all fragments available in activity will also be stopped. • Fragments were added to Android API in Honeycomb(3.0) version of Android which API version 11. • Earlier we had a limitation because we can show only a single activity on screen at one given point in time. So we were not able to divide device screen and control different parts separately. • But with fragment we got more flexibility and removed limitation of having a single activity on screen at a time. Fragments will have their own layout, events and complete lifecycle. • You create fragments by extending Fragment class and you can insert a fragment into your activity layout by declaring fragment in activity's layout file, as a <fragment> element.
  • 6. Main Building Blocks – Fragments
  • 7. Main Building Blocks – Fragment Lifecycle Phase I: When a fragment gets created, it goes through following states: • onAttach() // when a frag is attached to its hosting activity • onCreate() //frag is initialized, but no UI • onCreateView() //frag sets up and returns its UI. This view is given to hosting activity afterwards. • onActivityCreated() // Now frag’s life cycle is depending upon its hosting activity’s life cycle Phase II: When fragment becomes visible, it goes through these states: • onStart() //hosting activity is about to become visible • onResume() // hosting activity is about to become visible and ready for user interaction Phase III: When fragment goes into background mode, it goes through this states. • onPaused() //hosting activity is visible, but another activity is in the foreground and has focus • onStop() // When hosting activity is not visible
  • 8. Main Building Blocks – Fragment Lifecycle Phase IV: When fragment is destroyed, it goes through following states: • onPaused() • onStop() • onDestroyView() //hosting activity is about to be destroyed any frag that it is hosting also has to be shut down • onDestroy() //release frag resources • onDetach() //null out references to hosting activity Adding Fragments to Activities Two general ways 1. First, i. Fragment can be statically added to the activity’s layout file. ii. It is then used in a call to setContentView method. 2. Second, i. Add it programmatically using FragmentManager
  • 9. Main Building Blocks – How to use Fragments? This involves number of simple steps to create Fragments. • First of all decide how many fragments you want to use in an activity. For example let's we want to use two fragments to handle landscape and portrait modes of the device. • Next based on number of fragments, create classes which will extend the Fragment class. Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements. • Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the defined fragments. • Finally modify activity file to define actual logic of replacing fragments based on your requirement. A Useful Trick! • Press Ctrl and click on keyword of java in Android Studio. You will get complete definitions of that keyword.